Witch Doctor
Witch Doctor

Reputation: 73

Laravel Eloquent date(now) compare two date not working

I have this on Simple query

SELECT * FROM `blogs` WHERE DATE(NOW()) = DATE(updated_at)

and it shows result,

but when I try

$blog = Blog::whereDate('updated_at', '=', 'DATE(NOW())')->get();

$blog = Blog::where('DATE(NOW())','=', 'DATE(updated_at)')->get();

$blog = Blog::where(DB::RAW('DATE(NOW()) = DATE(updated_at)'))->get();

tried these three... still can't

Upvotes: 3

Views: 5189

Answers (1)

Kiril
Kiril

Reputation: 331

You can achieve this in several ways:

$blog = Blog::whereDate('updated_at', '=', date('Y-m-d'))->get();
$blog = Blog::whereDate('updated_at', '=', Carbon::today()->toDateString())->get();
$blog = Blog::whereRaw('DATE(NOW()) = DATE(updated_at)')->get();
$blog = Blog::where(DB::RAW('DATE(NOW())'), '=', DB::RAW('DATE(updated_at)'))->get();

P.S.

Also take a look on this:

whereDay('updated_at', '=', date('d'));
whereMonth('updated_at', '=', date('m'));
whereYear('updated_at', '=', date('Y'));

Upvotes: 3

Related Questions