Laravel雄辩的查询构建选择最小值 [英] Laravel Eloquent query build select min value

查看:209
本文介绍了Laravel雄辩的查询构建选择最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要查询以下内容(已整理)以列出要预订的房间:

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);

实际查询(未修剪):

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.

推荐答案

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': {
                //
            }
        }
    }
}

这就是你想要的吗?

这篇关于Laravel雄辩的查询构建选择最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆