43

Get The Max Date With Laravel Eloquent

 4 years ago
source link: https://www.codewall.co.uk/get-the-max-date-with-laravel-eloquent/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Get The Max Date With Laravel Eloquent

Getting the max date with Eloquent is executed by simply using the max() operator to get the maximum date time stamp in any given data table. The following example assumes the date column we are looking at is named created_at, but replace it with your own if needs be.

PHP

$lastRecordDate = Product::all('created_at')->max('created_at')->toArray();

dd($lastRecordDate);

Whilst this Eloquent query gets the maximum date available in the table, it actually gives us more information in the output, another magic gem in Laravel.

Output

last record in database laravel method 2

If you want to print the date time, it would be a case of the following line of code

echo $lastRecordDate["formatted"]; // outputs 2020-11-21 15:07:00

Hope this helps!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK