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


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!


Source: codewalk


The Tech platform

www.thetechplatform.com

0 comentarios

Entradas Recientes

Ver todo

Get The Current URL In Laravel Controller & View

Accessing the current URL can be done by making use of the global url variable that is accessible in Laravel controllers. It can be called directly or can be imported as a facade that is then used as

How To Show Validation Errors In Laravel Views

Validation feedback messages are an invaluable addition to any CRUD style application. If your app allows users to create records that are stored in your database, then you will of course want to vali