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

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello Dev’s
Today now in this example, I will help you to know how to get last record from database in Laravel 8 with a short example. You can easily get last record by using laravel 8 eloquent model.i will show you how to Laravel 8 get last record from database.

There is some good ways to get last record from database table in Laravel application.

In PHP language, we normally use order by clause with descending order to get last one record from database table and also we can do same in laravel. But laravel has some awesome and simple method to interact with database table in laravel application.

So we can do this by using latest method to get last record on table from database in Laravel application.

Method 1:

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

Method 2:

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

Read Also : Laravel 8 User Roles and Permissions Tutorial

Thanks For Reading . I hope it will help you……For more you can follow us on facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →