Sometimes we need to require to get only last record of table in our project, We can fetch it in several ways. We can fetch last record of database table using latest() or orderBy(). In bellow example. In this example you can see how i get the last record of table.
In this quick example of laravel get last record from collection i will show you three ways. You can choose one of them to use. If you don't know the query of Laravel get latest record by date, then just see the below code.
Example One
return \App\Customer::orderBy('id', 'desc')->first();
Example Two
\App\Customer::latest()->first();
Example Three
\App\Customer::all()->last();
Hope it can help you.
#laravel #laravel-7 #eloquent-tips