How to get last record from table in PHP Laravel 6 Framework?

Admin   PHP   437  2021-03-18 22:40:04

In this example, I will show you how to get last record from database in Laravel 6 with example. you can simply get last record using laravel 6 eloquent model.

There are several way to get last record from table in Laravel 6.

In PHP, you normally use order by clause with descending order to get last one record from database table and same you can do in laravel but laravel has some awesome method to interact with database table.

You can use latest method to get last record from database table in Laravel application.

Method 1:

 

$lastRecord = User::latest()->first();

dd($lastRecord);

Method 2:

 

$lastRecord = User::orderBy('id', 'DESC')->first();

dd($lastRecord);

It will help you....