2017-12-19 36 views
-1

私は、彼らがroom_types、部屋、料金、私は価格と料金で部屋や料金とLaravel雄弁とクエリビルダイーガーロード複数の関係where句

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

答えて