Laravel:使用Eloquent在查询中加入 [英] Laravel : Join within query using Eloquent

查看:71
本文介绍了Laravel:使用Eloquent在查询中加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个表,我需要从中获取所有数据. post,product,categories,attributes,post_attribute

I have a five tables in which i need to get data from all this. post,product,categories,attributes,post_attribute

我的模型中有一个查询,该查询收集一个产品的所有帖子,并且包含类别和属性详细信息. 查询:

I have a query in my model that collect all post for a one product and it contains categories and attributes details . Query :

 Post::with(['product.categories.attributes'])->whereStatus("Active")->get();

响应:

 "PostDetails": [
    {
        "id": 153,
        "user_id": 3,
        "product_id": 1,
        "demand_or_supply": "Demand",
        "description": "This is a post description three",
        "image_one": null,
        "image_two": null,
        "image_three": null,
        "image_four": null,
        "status": "Active",
        "created_at": "2018-05-07 09:51:08",
        "updated_at": "2018-05-07 09:51:08",
        "product": {
            "id": 1,
            "title": "Diamond",
            "icon": null,
            "status": "Active",
            "created_at": "2018-04-27 18:46:28",
            "updated_at": "2018-04-27 18:46:28",
            "categories": [
                {
                    "id": 1,
                    "product_id": 1,
                    "title": "Shape",
                    "status": "Active",
                    "sort_order": 1,
                    "created_at": "2018-04-27 18:46:28",
                    "updated_at": "2018-04-27 18:46:28",
                    "attributes": [
                        {
                            "id": 1,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "Round",
                            "status": "Active",
                            "sort_order": 2,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 2,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "Princess",
                            "status": "Active",
                            "sort_order": 1,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 3,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "Oval",
                            "status": "Active",
                            "sort_order": 3,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 4,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "Kite",
                            "status": "Active",
                            "sort_order": 8,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 5,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "1-5",
                            "status": "Active",
                            "sort_order": 4,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 6,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "6-10",
                            "status": "Active",
                            "sort_order": 9,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 8,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "-2",
                            "status": "Active",
                            "sort_order": 10,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 9,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "+2 -4",
                            "status": "Active",
                            "sort_order": 7,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        },
                        {
                            "id": 10,
                            "product_id": 1,
                            "category_id": 1,
                            "parent_id": null,
                            "title": "+4 -6 1/2",
                            "status": "Active",
                            "sort_order": 6,
                            "created_at": "2018-04-27 18:46:29",
                            "updated_at": "2018-04-27 18:46:29"
                        }
                    ]
                },

此表中还有一个名为post_attributes的表,我存储attribute_idproduct_id属于attributeproduct表.

There is another table called post_attributes in this table I store attribute_id and product_id which is belongs to attribute and product table.

我想过滤仅在post_attributes表中可用的attributes数据,并且仅过滤在post_attributes表中具有product_idattribute_id的'product'数据. 我该怎么办?

I want to filter attributes data which is only available in post_attributes table and only for 'product' which is available on post_attributes table with product_id and attribute_id. How can i do this ?

推荐答案

要约束急切加载关系,您可以将闭包传递给急切加载查询,而要查询关系,则可以使用whereHas函数,因此看起来像这样的东西:

To constraint eager loading relations you can pass a closure to the eager loading query, and to query a relation you can use whereHas function, so it would look something like this:

Post::with(['product.categories.attributes' => function($query) {
    // Eager load constraint
    $query->whereHas('post_attribute', function ($query) {
        $query->where('product_id', 1); // Filter by the joined data
    });
}])->whereStatus("Active")->get();

希望这对您有所帮助.

这篇关于Laravel:使用Eloquent在查询中加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆