我正在使用 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/