代码之家  ›  专栏  ›  技术社区  ›  Javed

Laravel:使用Eloquent在查询内连接

  •  1
  • Javed  · 技术社区  · 6 年前

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

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

     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_id product_id 属于 attribute product 桌子

    我要筛选 attributes 仅在中可用的数据 post\u属性 表,并且仅适用于 post\u属性 具有的表格 product\u id 属性\u id 。 我该怎么做?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Asur    6 年前

    若要约束渴望加载关系,可以将闭包传递给渴望加载查询,若要查询关系,可以使用 whereHas 函数,所以它看起来像这样:

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

    希望这对你有帮助。