Laravel eloquent和查询构建器Eager用where子句加载多个关系

时间:2017-12-19 10:10:06

标签: laravel laravel-eloquent laravel-query-builder

我有几张桌子,他们是room_types,房间,价格,我想要的价格

room_types,房间价格和价格在哪里(价格room_type_id = room_types.id)。我试着做那样的事情

        $roomTypeIds = [];

        foreach ($hotel->rooms as $room) {
            array_push($roomTypeIds, $room->roomtype->id);
        }

        $roomsByType = RoomType::with(['rooms' => function ($query) {
            $query->with(['rates' => function($q) {
                $q->with('policy', 'prices');
            }]);
        }])->whereIn('id', array_unique($roomTypeIds))->get();

但是在这个例子中我得到了所有的价格,但我想得到的只是room_type_id等于当前room_type.id的价格。所以为此,我试图做一些这样的想法

        $roomsByType = RoomType::with(['rooms' => function ($query) {
            $query->with(['rates' => function($q) {
                $q->with(['policy', 'prices' => function($q1) {
                    $q1->where('room_type_id', '');
                }]);
            }]);
        }])->whereIn('id', array_unique($roomTypeIds))->get();

但我不知道该把什么放在clouse的地方。有没有办法做到这一点? 我希望得到的结果是

[
  {
    "id": 2,
    "name": "Twin/Double",
    "created_at": "2017-12-11 08:56:16",
    "updated_at": "2017-12-11 08:56:16",
    "rooms": [
      {
        "id": 4,
        "hotel_id": 1,
        "room_type_id": 2,
        "custom_name": null,
        "room_name": "3",
        "number_of_type": 2,
        "number_of_bedrooms": null,
        "number_of_livingrooms": null,
        "number_of_bathrooms": null,
        "created_at": "2017-12-12 06:37:34",
        "updated_at": "2017-12-12 06:37:34",
        "rates": [
          {
            "id": 4,
            "user_id": 19,
            "custom_name": "Default rate",
            "price": 0,
            "automatic": 0,
            "rate_id": null,
            "operand": null,
            "amount": null,
            "currency": null,
            "policy_id": 1,
            "meal_types": null,
            "created_at": "2017-12-12 09:27:31",
            "updated_at": "2017-12-12 09:27:29",
            "pivot": {
              "room_id": 4,
              "rate_id": 4
            },
            "policy": {
              "id": 1,
              "name": "Free cancellation before 3 / 60 %",
              "hotel_id": 1,
              "free": 1,
              "before_day": 3,
              "before_day_price": 60,
              "until_day_price": null,
              "created_at": "2017-12-08 14:03:31",
              "updated_at": "2017-12-08 14:03:31"
            },
            "prices": [
              {
                "id": 1,
                "rate_id": 4,
                "room_type_id": 2,
                "from": "2017-12-01 09:18:46",
                "to": "2017-12-18 09:18:57",
                "amount": 100,
                "created_at": "2017-12-18 09:19:11",
                "updated_at": "2017-12-18 09:19:12"
              },
              {
                "id": 3,
                "rate_id": 4,
                "room_type_id": 3,
                "from": "2017-12-22 10:36:30",
                "to": "2017-12-30 10:36:35",
                "amount": 3000,
                "created_at": null,
                "updated_at": null
              }
            ]
          }
        ]
      },
      {
        "id": 5,
        "hotel_id": 1,
        "room_type_id": 2,
        "custom_name": null,
        "room_name": "3",
        "number_of_type": 2,
        "number_of_bedrooms": null,
        "number_of_livingrooms": null,
        "number_of_bathrooms": null,
        "created_at": "2017-12-12 06:37:34",
        "updated_at": "2017-12-12 06:37:34",
        "rates": [
          {
            "id": 4,
            "user_id": 19,
            "custom_name": "Default rate",
            "price": 0,
            "automatic": 0,
            "rate_id": null,
            "operand": null,
            "amount": null,
            "currency": null,
            "policy_id": 1,
            "meal_types": null,
            "created_at": "2017-12-12 09:27:31",
            "updated_at": "2017-12-12 09:27:29",
            "pivot": {
              "room_id": 5,
              "rate_id": 4
            },
            "policy": {
              "id": 1,
              "name": "Free cancellation before 3 / 60 %",
              "hotel_id": 1,
              "free": 1,
              "before_day": 3,
              "before_day_price": 60,
              "until_day_price": null,
              "created_at": "2017-12-08 14:03:31",
              "updated_at": "2017-12-08 14:03:31"
            },
            "prices": [
              {
                "id": 1,
                "rate_id": 4,
                "room_type_id": 2,
                "from": "2017-12-01 09:18:46",
                "to": "2017-12-18 09:18:57",
                "amount": 100,
                "created_at": "2017-12-18 09:19:11",
                "updated_at": "2017-12-18 09:19:12"
              },
              {
                "id": 3,
                "rate_id": 4,
                "room_type_id": 3,
                "from": "2017-12-22 10:36:30",
                "to": "2017-12-30 10:36:35",
                "amount": 3000,
                "created_at": null,
                "updated_at": null
              }
            ]
          }
        ]
      }
    ]
  }
]

1 个答案:

答案 0 :(得分:0)