php - 如何编写 Laravel Eloquent whereBetween 方法与两列

标签 php mysql laravel-5

我正在尝试以 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/

相关文章:

php - 服务器上传文件404错误

php - MySQL:无法使用 WHERE 子句返回 POINT 类型

php - Laravel:简单表单获取请求返回 500 错误

javascript - Laravel 和 Ajax 如何处理接收到的数据并将其保存在数据库中?

laravel - 如果 array_diff 在 Collection->toArray() 上返回 assoc 数组,则 JSON 是对象而不是数组

php - 如何使用 php 包含文件将事件类添加到元素

php - 在 while 循环中回显文本字段值

javascript - 如果数据库中有新条目,聊天更新

MySQL 错误 1064 - 正确的语法?

javascript - php 检索数据以在 HTML 中填充 DIV