【问题标题】:Laravel 6 Eloquent Query Using WHERE with AND OR AND?Laravel 6 Eloquent Query 使用 WHERE 和 AND OR AND?
【发布时间】:2020-06-27 13:50:44
【问题描述】:

我想显示目前营业的餐厅。我在 laravel 中怎么说:

WHERE (open = '00:00:00' and close = '00:00:00' ) OR (open < $currentTime and close > $currentTime)

我是这样写的:

->where(function ($query) {
    $query->where('owh.open', '=', '00:00:00')
          ->whereAnd('owh.close', '=', '00:00:00');
})->orWhere(function ($query) use ($currentTime) {
    $query->where('owh.open', '<', $currentTime)
          ->whereAnd('owh.close', '>', $currentTime);
})

但它没有给我正确的数据。

【问题讨论】:

    标签: laravel eloquent query-builder laravel-6


    【解决方案1】:

    我找到了自己问题的答案

    ->whereIn('objects.id', [
                DB::raw("SELECT object_id FROM (SELECT object_id,
                       (CONCAT('$date', ' ', open)::timestamp) as open,
                       (
                            CASE WHEN open > close THEN
                                (CONCAT('$date', ' ', close)::timestamp + interval '1' day)
                            ELSE (CONCAT('$date', ' ', close)::timestamp) END
                        )
                        as close
                FROM object_work_hours WHERE type=$currentWorkHourType AND deleted_at IS NULL) as tb1 WHERE '$currentDate'::timestamp BETWEEN open AND close OR (open::time = '00:00:00' AND close::time = '00:00:00')")
            ])
    

    $currentWorkHourType = date('N');
    $currentDate = Carbon::now()->format('Y-m-d H:i:s');
    $date = date('Y-m-d');
    

    【讨论】:

      【解决方案2】:

      在下面使用

      ->where(function ($query) {
          $query->where('owh.open', '=', '00:00:00')
              ->where('owh.close', '=', '00:00:00');
      })->orWhere(function ($query) use ($currentTime) {
          $query->where('owh.open', '<', $currentTime)
              ->where('owh.close', '>', $currentTime);
      })
      

      【讨论】:

        猜你喜欢
        • 2013-06-04
        • 1970-01-01
        • 2012-11-06
        • 2016-07-05
        • 2020-01-01
        • 1970-01-01
        相关资源
        最近更新 更多