Get the Last Inserted Record in Laravel

Throughout the Laravel get last inserted record tutorial you will learn how to get the last record inserted into table in laravel application. The last inserted data means which is inserted the last time in the database, So we can say the get the latest inserted record from the database in Laravel.

We have two eloquent methods to get the last inserted record from the database table in Laravel.

Method 1: Using the latest() method

Here we have a posts table and we get the last record from the post table using the latest() with the first() methods. The latest() will fetch only the latest record according to created_at and the first() will get only a single record.

$lastRecord = Post::latest()->first();

Method 2: Using Order By

Using orderBy with DESC, It’s deceased the table and then get the first which is the latest record.

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

I hope these two methods work for getting the last (latest) record from the table in laravel.

Leave a Comment