How to get Last Record from Table in Laravel App

Laravel get last inserted record example; In this tutorial we will show you how to get Last Record from database table in Laravel application. There are several way to get latest inserted record from table in Laravel. We can use latest()->first() and orderBy() method to get last record table Laravel project.

Normally we can use order by clause along with limit 1 to get last row from database table in laravel app. The easy and simple examples to get last record which is inserted in last time.

1. Using Order By belong to id field

Here we used orderBy() that belong to id, it will always return last record from table.

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

2. Using latest() belong to created_at field

The latest method return last record which is created in last time and the the first get the one record.

$lastestRecord = DB::table('users')->latest()->first();

3. Using Order By created_at field

$lastestRecord = DB::table('users')->orderBy('created_at', 'DESC')->first();

I hope you got your solution of how to get last inserted record in laravel app.

Leave a Comment