How to get last record of table in Laravel 7.x and 6.x?

April 16, 2017 | Category : Laravel 5.5 Laravel 5 Laravel PHP

Here, i will show you how to select last row of database table using Laravel query builder. In PHP Laravel framework, there are several eloquent method to get last record from table.

In this article, I will show you two way to getting last row of the table. In this example i going to use following method of laravel eloquent.

1)first()

2)orderBy()

3)latest()

Above three method through we can simply select last record of any table of database, here i gave you "posts" table for example. As bellow you can see first way using "order by".

Using Order By:

$lastRecord = DB::table('posts')->orderBy('id', 'DESC')->first();

now as bellow, i will get last record using latest() method:

Using latest method:

$lastRecord = DB::table('posts')->latest()->first();

As above example will help you to fetch last record of table.