我正在尝试检索酒店列表,其中哪些房间可用于请求的日期。
房间可预订量表如下所示。
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,但添加后返回空集。
我必须添加或删除什么?
答案 0 :(得分: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();
答案 1 :(得分: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();
该集合应按酒店代码和房间代码进行分组,在这种情况下,酒店数据可能会重复,并且可以在控制器上进行过滤。