I want to fetch only today’s post using eloquent in Laravel. Right now I am using the code below. Is there any better way to do that?
$posts = Post::where('created_at', '>=', date("Y-m-d H:i:s"))->get();
I want to fetch only today’s post using eloquent in Laravel. Right now I am using the code below. Is there any better way to do that?
$posts = Post::where('created_at', '>=', date("Y-m-d H:i:s"))->get();
I am not sure if this is the better version of getting today’s posts. If you want you can try this.
$posts = Post::whereDate('created_at', Carbon::today())->get();
If you are using an old version of Laravel you might get an error whereData missing argument 3. In that case, just add an ‘=’ like below.
$posts = Post::whereDate('created_at', '=', Carbon::today())->get();