Laravel : Join within query using Eloquent

不想你离开。 提交于 2020-01-25 03:50:11

问题


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();

Response:

 "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"
                        }
                    ]
                },

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.

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 ?


回答1:


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();

Hope this helps you.



来源:https://stackoverflow.com/questions/50215037/laravel-join-within-query-using-eloquent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!