我正在尝试以 Eloquent 格式编写以下查询:
SELECT * FROM `table_name` where timestamp_col1 AND timestamp_col2 BETWEEN
'2018-07-23 11:45:25' and '2018-07-23 14:45:25'
通过使用 eloquent 的默认方法,我能够按以下格式编写查询:
$this->getEntity()
->whereBetween('timestamp_col1', [$start, $end])
->orWhereBetween('timestamp_col2', [$start, $end])
->get();
但是用上面的格式写的缺点是我必须手动指定
两列分别开始和结束时间戳。有没有其他方法可以实现此目的,还是我必须使用 whereRaw()
方法?谢谢。
最佳答案
您不需要手动指定$start
和$end
时间戳。您只需要使用 Php 日期函数将 $start
和 $end
值转换为时间戳或正确的日期时间格式,如下所述:
$start = date('Y-m-d H:i:s', strtotime($start));
$end = date('Y-m-d H:i:s', strtotime($end));
$this->getEntity()
->whereBetween('timestamp_col1', [$start, $end])
->orWhereBetween('timestamp_col2', [$start, $end])
->get();
关于php - 如何编写 Laravel Eloquent whereBetween 方法与两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51464060/