问题描述
我有一个查询使用Laravel的Eloquent和我需要一个其中
子句,将获取自当前时间戳后的最后8小时的数据。我可以很容易地在一个原始查询如:
I have a query using Laravel's Eloquent and I need a where
clause that will get the data from the last 8 hours since the current timestamp. I can easily do that in a raw query like:
SELECT * FROM task_tracker.tasks WHERE created_at > DATE_ADD(NOW(), INTERVAL -8 HOUR);
但是如何以一个雄辩的格式呢? tasks.created_at
是日期时间格式。我目前有这个:
But how do I do that in an eloquent format? The tasks.created_at
is a Datetime Format. I have this currently:
$tasks = Task::join('users', 'tasks.added_by', '=', 'users.id')
->join('users AS x', 'x.id', '=', 'tasks.assigned_to')
->select([
'tasks.id',
'tasks.task_description',
'tasks.start_timestamp',
'tasks.end_timestamp',
'tasks.status',
'users.name',
'x.name AS assign',
'tasks.created_at'
// ])->where('tasks.created_at', '>', 'DATE_ADD(NOW(), INTERVAL -8 HOUR)');
])->where(DB::raw('tasks.created_at', '=', 'DATE_ADD(NOW(), INTERVAL -8 HOUR)' ));
dd($tasks);
我尝试使用 DB :: raw
在一个单引号(注释行)中的简单encloure,但不工作,不获取任何数据。我使用MySQL。
I tried using DB::raw
and a plain encloure in a single quote (the commented line) but does not work and doesn't get any data. I am using MySQL.
推荐答案
DB :: raw
有一个参数所以尝试 whereRaw
就像这样:
DB::raw
has one parameter so try whereRaw
like this:
- > whereRaw('tasks.created_at = DATE_ADD(NOW(),INTERVAL -8 HOUR)
这篇关于从datetime列中的最后8小时获取数据Eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!