hello
Just with orderBy, for example :
Post::orderBy('created_at', 'desc')->first();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello ,
How i can select the most recent date with laravel ?
hello
Just with orderBy, for example :
Post::orderBy('created_at', 'desc')->first();
You could also use the latest() method in Laravel 5.
Post::latest()->get();
@deringer I didn't know this short syntax ! It adds an orderBy to the query, so I also found the oldest that gets the oldest one.
@deringer thank you for the answer , but i want to select Closest to today's date ??
@SeifBh I think the last date should be the closest to today's date...
If you have future dates, I suppose you might do something like
// First record after now
Model::where('created_at', '>=', $yourDate)->orderBy('created_at', 'asc')->first();
// First record before now
Model::where('created_at', '<=', $yourDate)->orderBy('created_at', 'desc')->first();
And that will give you the first record in the database after now. Without context in your app, it's hard to say if this will achieve what you're after though.
I must say I dont really understand this date story ^^
@bestmomo lol , i build a football website , and i want to display the closest to today's date... Note : i insert this date manually with phpmyadmin what solution you propose
Man try to learn by yourself. We wont build your website instead of you.
@pmall thank you for the advice , but this website for people like me .
Start by reading eloquent documentation and watch screecasts about eloquent. Then if you still have questions we can help.
Thankyou @pmall
@SeifBh @deringer @bestmomo This is how you get timestamp closest to now:
// mysql with second precision
Model::orderByRaw(' abs( timestampdiff(second, current_timestamp, created_at)) asc ')->first();
// postgres with second precision
Model::orderByRaw(' abs( extract(epoch from now()) - extract(epoch from created_at) asc ')->first();
Please or to participate in this conversation.