Reputation: 788
I want to filter a table by user ID and the created_at field. The only thing is created_at is a timestamp, and I want to query by date only not time.
So something like -
$messages = Message::where(['from' => $fromUser->id, 'created_at' => 'TODAY'S DATE'])->get();
Upvotes: 12
Views: 22253
Reputation: 7689
$query->whereDate('created_at', '=', date('Y-m-d'));
or
$query->whereDate('created_at', '=', Carbon::today()->toDateString());
Upvotes: 20
Reputation: 5246
The only way I know (in Laravel) to compare a DATE
against created_at
is to use whereRaw
, so for your example, you'd use something like:
Message::where(...)->whereRaw('DATE(created_at) = ?', [$today])->get();
You can find more information about Eloquent and it's other methods here
Upvotes: 7