0

我正在尝试检索酒店列表,在哪些房间可用于请求的日期。

空房情况表如下所示。

room_availability

id |    hotel   |     room      | start_date |   end_date  | count | 
--------------------------------------------------------------------
1  |     301    |      121      | 2019-04-01 |  2019-04-01 |   10  |
2  |     301    |      121      | 2019-04-02 |  2019-04-02 |   7   | 
3  |     301    |      121      | 2019-04-03 |  2019-04-03 |   4   | 
4  |     301    |      120      | 2019-04-02 |  2019-04-02 |   5   | 
5  |     301    |      120      | 2019-04-03 |  2019-04-03 |   6   |  

搜索模型代码为,


$no_of_days = (Carbon::parse($data['start_date'])
              ->diffInDays(Carbon::parse($data['end_date'])));
$no_of_days += 1;

$hotelList = $this
        ->select('id','hotel_code','room', \DB::Raw('count(hotel_code) as total_days'))
        ->with(['hotel:id,name,logo,location,code'])
        ->where('start_date','>=',$data['start_date'])
        ->Where('start_date','<=',$data['end_date'])
        ->groupBy('hotel_code')
        ->having('total_days',$no_of_days)->get();

对于没有条件的请求(2019-04-02 - 2019-04-03),

->having('total_days',$no_of_days)

返回酒店 301 但添加时返回空集。

我必须添加或删除什么?

4

2 回答 2

0

以下查询将预期数据返回给我,

$hotelList = $this
        ->select('id','hotel_code','room', \DB::Raw('count(hotel_code) as total_days'))
        ->with(['hotel:id,name,logo,location,code'])
        ->where('start_date','>=',$data['start_date'])
        ->Where('start_date','<=',$data['end_date'])
        ->groupBy('hotel_code','room')
        ->having('total_days',$no_of_days)->get();

该集合应按hotel_code 和房间代码分组,在这种情况下,酒店数据可能会重复,可以在控制器上进行过滤。

于 2019-03-16T09:49:07.233 回答
0

试试下面的查询,

$no_of_days = (Carbon::parse($data['start_date'])
          ->diffInDays(Carbon::parse($data['end_date'])));
$no_of_days += 1;

$hotelList = $this
    ->select('id','hotel_code','room', \DB::Raw('count(hotel_code) as total_days'))
    ->with(['hotel:id,name,logo,location,code'])
    ->where('start_date','>=',$data['start_date'])
    ->Where('start_date','<=',$data['end_date'])
    ->groupBy('hotel_code')
    ->havingRaw('count(hotel_code) ='. $no_of_days)->get();
于 2019-03-14T14:22:59.283 回答