【问题标题】:Laravel eloquent and query builder Eager Loading Multiple Relationships with where clauseLaravel 雄辩的和查询构建器渴望加载带有 where 子句的多个关系
【发布时间】:2018-06-01 17:53:53
【问题描述】:

我有几张桌子,它们是 room_types、房间、价格、价格我想要得到的

room_types 房间的价格和价格的价格 where(price 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
              }
            ]
          }
        ]
      }
    ]
  }
]

【问题讨论】:

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


    【解决方案1】:

    【讨论】:

    • 我必须在 where 子句中写同样的错误。
    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 2015-09-18
    • 2019-12-09
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    相关资源
    最近更新 更多