【问题标题】:Laravel : Eloquent HAVING returns invalid data setLaravel:雄辩的 HAVING 返回无效的数据集
【发布时间】:2019-03-14 13:55:25
【问题描述】:

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

空房情况表如下所示。

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 但添加时返回空集。

我必须添加或删除什么?

【问题讨论】:

  • ->having(\DB::Raw('count(hotel_code),$no_of_days) 试试看
  • -&gt;having(\DB::Raw('count(hotel_code)'),$no_of_days) 相同的回复@JoaoGRRR
  • 使用 ->having('total_days', '=' ,$no_of_days)
  • 谢谢@pal,我的回答(在下面)有效

标签: laravel eloquent eloquent-relationship


【解决方案1】:

试试下面的查询,

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

【讨论】:

  • 返回空集@Sandeep。
  • 您能否确认您有满足条件count(hotel_code) ='. $no_of_days的数据
  • yes @sandeep,通过删除条件,返回 total_days 和 $no_of_days 为 2。对于 (2019-04-02 - 2019-04-03)
  • 酷。能否请您返回查询并在您的 sql 浏览器上运行它。查询看起来不错。可能有机会错过其他东西。在查询构建器上方使用 /DB::enableQueryLog(),在查询构建器下方使用 dd(/DB::getQueryLog());。你会得到查询。
  • 在@Sandeep 中测试,单独使用这个WHERE `start_date` &gt;= '2019-04-01',删除了所有其他条件。我很惊讶得到如下单行结果1 | 301 | 121 | 5
【解决方案2】:

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

$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和房间代码分组,在这种情况下酒店数据可能会重复,可以在控制器上过滤。

【讨论】:

    猜你喜欢
    • 2020-02-18
    • 2018-06-19
    • 2021-06-23
    • 2020-09-08
    • 2015-12-17
    • 2021-09-15
    • 2016-11-25
    • 2017-01-26
    • 2016-07-03
    相关资源
    最近更新 更多