我在我的项目中面临一个问题。我有两张桌子的酒店,酒店房间。我想根据从酒店房间得到的数据来orderBy酒店。例如
在我的酒店桌上有两个酒店A,酒店B和酒店A有3个房间,价格是300,400,500,酒店B的价格是600,700,800。我想按价格从高到低的顺序展示酒店。
以下是我的代码
if($request->fprice) {
$fprice = $request->fprice;
} else {
$fprice = 'asc';
}
$hotels = Hotel::active()->with('ratings')
->with(['rooms' => function ($query) use ($room_type, $checkin, $checkout, $fprice) {
if ($room_type) {
$query->where('room_type_id', $room_type->id);
}
$query->orderBy('default_price', $fprice);
}])->paginate($this->perpage);提前感谢
发布于 2018-10-20 14:54:30
在这种情况下,您可能希望使用whereHas(),如下所示:
$hotel = Hotel::whereHas('HotelRoom', function($query) {
$query->orderBy('price', 'DESC');
})->get();加上您的查询中的任何其他条件。
参考:https://laravel.com/docs/5.7/eloquent-relationships#querying-relations
https://stackoverflow.com/questions/52902850
复制相似问题