[英]Laravel Eloquent query build select min value
I am having the following query (trimmed) to list the rooms to user for booking: 我正在使用以下查询(已整理)列出要预订的房间供用户使用:
$buildquery=Room::
with(['hotel' => function ($query) {
$query->where('status', 0);
}])
->with('image')->with('amenities');
if ($request->filled('location_id')) {
$buildquery->Where('city', $request->location_id);
}
$buildquery->Where('astatus', 1)->Where('status', 0);
$rooms = $buildquery->simplePaginate(20);
Actual query (not trimmed): 实际查询(未修剪):
select `rooms`.*,
(select count(*) from `amenities` inner join `amenities_room` on `amenities`.`id` = `amenities_room`.`amenities_id` where `rooms`.`id` = `amenities_room`.`room_id` and `amenities_id` in (?)) as `amenities_count`
from
`rooms`
where `city` = ? and `price` between ? and ? and `astatus` = ? and `status` = ? having
`amenities_count` = ?
limit 21 offset 100
It lists all the rooms available in hotel. 它列出了酒店所有可用的客房。 I need to select only one room for one hotel with least price.
我只需要以最低的价格为一家酒店选择一个房间。
You can use order by
您可以
use order by
方式use order by
$buildquery->orderBy('COL_NAME', 'DESC')->get();
if you need only one you can use take(1)
如果只需要一个,可以使用
take(1)
Hotel::with('room' => function($query) {
$query->orderBy('price', 'asc')->first();
},
'room.images',
'room.roomTypes',
'room.amenities'])
->get();
You can do something like this to get structure like: 您可以执行以下操作以获得以下结构:
{
'Hotel': {
'Room': {
'Images': {
//
},
'roomTypes': {
//
},
'amenities': {
//
}
}
}
}
Is that what you want? 那是你要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.