How to get last record from table in Laravel?

Here, I will show you how to get the last record from the database in Laravel. you can simply get the last record using the Laravel built-in eloquent model.

There are several ways to get the last record from the table in Laravel.

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

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

Example 1:

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

Example 2:

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

Thank you for following us and please drop a comment if it’s solved your problem and you learn something new. Thank you and have a great day ahead.

You May Also Like

About the Author: Sumanta Kabiraj

Leave a Reply

Your email address will not be published. Required fields are marked *