Different ways to Get the Last Inserted Id Using Laravel Eloquent

Different ways to Get the Last Inserted Id Using Laravel Eloquent

Hello Devs,

In Laravel Eloquent ORM, when we insert a new record into a database table that has an auto-incrementing ID, we often want to retrieve the ID of the newly inserted record. Here are various ways to get the last inserted ID using Laravel Eloquent. We can use the bellow process for any laravel versioons.

 

 

 

Step - 1: Using the create Method

 

It's one of the mostly populated way to get the last inserted id.

$user = User::create(['name' => 'Web Journey'],['email' => 'webjourney@gmail.com']);
$last_inserted_id = $user->id;

 

 

Step - 2: Using the save Method

 

$user = new User;
$user->name = 'Web Journey';
$user->email= 'webjourney@gmail.com';
$user->save();
$last_inserted_id = $user->id;

 

 

Step - 3: Using the insertGetId Method

 

This method is actually part of the Query Builder rather than Eloquent, but it's worth mentioning

$last_inserted_id = DB::table('users')->insertGetId(['name' => 'Web Journey']);

 

 

Step - 4: Using lastInsertId with PDO

 

If we are using PDO to insert the data, we can use the lastInsertId method. This is more on the raw side of things and isn't a common way to do things in Laravel, but it's still possible.

DB::insert("INSERT INTO users (name) VALUES (?)", ['Web Journey']);
$last_inserted_id = DB::getPdo()->lastInsertId();

 

 

Step - 5: Using Model Events

 

We can also make use of Eloquent model events, like the created event, to get the last inserted ID. However, this is more for running some code after a record has been created rather than specifically for getting the ID. The ID will be available on the model instance within the event.

 

 

Step - 6: Using Database Transactions

 

If we are performing multiple operations and want to ensure they all succeed or fail together, we can use database transactions. Within the transaction, after creating a record, we can retrieve its ID

 DB::transaction(function () {
    $user = User::create(['name' => 'Web Journey']);
    $last_inserted_id = $user->id;
    // Other operations...
});

 

 

Keep in mind that the primary way to retrieve the last inserted ID in Eloquent is through the model instance itself after the record has been persisted to the database. The ID is automatically set on the instance.

 

 

 

You May Also Like Bellow Articles:

 

 

 

Laravel 9 live search data in a table using ajax. 

 

How to send SMS in laravel 9 using Twilio SMS API-Webjourney

 

Laravel 9 pdf invoice generate and download with barryvdh dompdf

 

How to create multi language website by laravel 9

 

Laravel 10 multiple form validation on the same page-WebJourney

 

 Laravel 10 Breeze Authentication - WebJourney

 

Laravel 10 Ajax jQuery Crud with Pagination and Live Search

 

Laravel Naming Conventions Accepted by Laravel community

 

Laravel Shorter and More Readable Syntax - WebJourney

Tags

  • Share This: