在这里,我有下面的代码,我想过滤我的酒店并为每家酒店选择最低价格的房间:
$city_id = $request->get('city_id');
$stars = $request->get('grade_stars');
$type_id = $request->get('accommodation_type_id');
$from_date = $request->get('from_date');
$to_date = $request->get('to_date');
$data = Accommodation::with(['city','accommodationRoomsLimited.roomPricingHistorySearch' =>function($query) use($from_date,$to_date){
$query->whereDate('from_date', '<=', $from_date);
$query->whereDate('to_date', '>=', $to_date);
}])
->whereIn('city_id',$city_id)
->whereIn('grade_stars',$stars)
->orWhere('accommodation_type_id',$type_id)
->paginate(10);
return $data;
这是我的住宿模式:
public function accommodationRoomsLimited()
{
return $this->Hasmany(AccommodationRoom::class)->select('id', 'accommodation_id');
}
在我的住宿房间模型中:
public function roomPricingHistorySearch()
{
return $this->hasOne(RoomPricingHistory::class,'accommodation_room_id','id')->orderBy('sales_price','ASC');
}
我有一个叫做房间定价历史的表,它记录了每天的房间价格,这是我在 api 中的输出,我想在 room_pricing_history_search 中选择 1 个具有最低 sales_price 的房间:
"accommodation_rooms_limited": [
{
"id": 4859,
"accommodation_id": 47,
"room_pricing_history_search": {
"id": 15741,
"accommodation_room_id": 4859,
"net_price": null,
"sales_price": 50,
"created_at": "2019-05-25 13:30:00",
"updated_at": "2019-05-25 13:30:00"
}
},
{
"id": 4860,
"accommodation_id": 47,
"room_pricing_history_search": {
"id": 4990,
"accommodation_room_id": 4860,
"net_price": null,
"sales_price": 1760000,
"created_at": "2019-05-25 13:30:00",
"updated_at": "2019-05-25 13:30:00"
}
},
Smart猫小萌
翻过高山走不出你