php - Laravel - 针对数据库中的日期范围的 PHP/MySQL 日期范围过滤器

标签 php mysql sql laravel

我正在使用 Laravel 开发一个应用程序,并尝试从数据库中过滤记录。标准如下:

在数据库中,我有 2 个日期列 [excluded_period_start][excluded_period_end]。两列都有日期数据类型。

现在我的表单中有 2 个字段 [start_date][end_date]

我想获取数据库中存储的除时间段之外的所有记录。我使用的代码是:

      $hotels = Hotel::whereHas('location' , function($query) use($searchOptions){


            if(trim($searchOptions['location']) != ''){
              $query->where('location_title', $searchOptions['location']);
            }

          })
          ->where('excluded_period_start', '<', $start)
          ->where('excluded_period_end', '>', $end)
          ->where('active', 1)
          ->take(10)
          ->paginate(10);

但是,这仅给出了数据库中存储的范围之间的结果,但我希望结果超出该范围。

我尝试了很多类似 ->whereBetween() 的方法,但没有一个起作用。

如有任何帮助,我们将不胜感激。

最佳答案

您的初始排除_期间不能介于开始和结束之间:

          ->whereNotBetween('excluded_period_start', [$start, $end])
          ->whereNotBetween('excluded_period_end', [$start, $end])

关于php - Laravel - 针对数据库中的日期范围的 PHP/MySQL 日期范围过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829795/

相关文章:

php - 代码点火器 : Difference Between two time stamps

php - 嵌套数组/连接可以用 Php::PDO 完成吗?

mysql - 重命名 MySQL 数据库

一个设备的MySQL启动和停止时间

mysql - Correlated Subqueries - Counting records "less than"主查询记录

php - 在 Magento 中获取订单增量 ID

php - 用匹配的字母替换字符串中的随机字母

php - 如何将数据从 Xcode 8 发送到 PHP MySQL 数据库?

mysql - 对多个计数字段求和

sql - 数据库架构设计问题