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

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

In this post we will give you information about How to get last record from table in PHP Laravel Framework?. Hear we will give you detail about How to get last record from table in PHP Laravel Framework?And how to use it also give you demo for it if it is necessary.

In this example, I will tell you how to select last row from database table in Laravel with example.

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

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.


Get last record from table using orderBy clause

  1. $last_row=DB::table('products')->orderBy('id','DESC')->first();
$last_row=DB::table('products')->orderBy('id', 'DESC')->first();

By running above command, record will be in descending order and you will get first row from record set of table.


Get last record from table using latest method

  1. $last_row= DB::table('products')->latest()->first();
$last_row = DB::table('products')->latest()->first();

By running above command, you will get last inserted record according to created_at column.

If you need to get last inserted id in Laravel then click below :


How to get last inserted id in laravel PHP Framework?

Hope this code and post will helped you for implement How to get last record from table in PHP Laravel Framework?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

85  +    =  91

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US