In this tutorial explain of how to get last inserted id in laravel. I explain simply about get inserted id. You frequently need to obtain the recently added model ID when working on your laravel application in order to assign it to another model. Here are various step to get the last inserted ID using laravel eloquent.
In laravel, after inserting data into a database table, you need to retrieve last inserted ID after creating the record. This article blog post will provide several method to get the last inserted ID using eloquent models, the DB facade and directly accessing the PDO instance.
Below example to get latest id in laravel.
1. Retrieving insert ID using Model::create
One common method to insert data into the database and get the data last inserted ID using the create method on a eloquent model. This method inserted the data but also fetches the last inserted ID. you can consider following example.
<?php
$user = user::create([
'name' => 'Jasminkumar',
'email'=> 'boradjasminkumar123@gmail.com',
'password' => bcrypt('12345');
]);
$last_inserted_id = $user->id;
?>
Explanation:
- Get all the request from form.
- Use Eloquent create() method to create a new record.
Use an accessor to get the id, and assign it to the $last_inserted_id.
Why can’t we use the Query Builder’s insert() method?
<?php
$user = user::insert([
'name' => 'Jasminkumar',
'email'=> 'boradjasminkumar123@gmail.com',
'password' => bcrypt('12345');
]);
?>
Using the above insert() method would only return a Boolean of true or false depending on the success of the query.
Read also: How to remove key from array in Laravel?
2. Retrieving insert ID using new Model() and save()
Another method to insert data and get last inserted ID is by creating a new instance of the model, setting the attributes, and then use save method. You can easily access the ID as a property with $user->id. You can consider following example.
<?php
$user = new User();
$user->name = 'Jasminkumar';
$user->email = 'boradjasminkumar123@gmail.com';
$user->password = bcrypt('12345');
$user->save();
$last_inserted_id = $user->id;
?>
3. Using DB Facade
If you want to add a new record to your model or table using the DB facade in laravel and retrieve the last inserted ID. You can use the insertGetId() method. This method use to easily get last inserted ID in laravel eloquent.
<?php
$last_inserted_id = User::insertGetId([
'name' => 'Jasminkumar',
'email' => 'boradjasminkumar123@gmail.com'
]);
?>
4. Using getPDO() method
if you want to know the ID of the last added item in a table using laravel. First you add a record to the table after that called PDO object. Also you can use the lastInsertedId() method on the PDO object to the get that ID.
<?php
$user = DB:table('users')->insert(
['name' => 'Jasminkumar', 'email' => 'boradjasminkumar123@gmail.com']
);
$user_id = DB::getPdo()->lastInsertId();
dd($user_id);
?>
Explanation:
This approach directly calls the PDO instance and retrieves the last inserted ID. This is useful if you’re working with raw SQL queries or want a quick method to get the last inserted ID from complex query.
5. Using latest() and first() with Eloquent
If you need the last inserted ID but don’t have immediate access to it, you can retrieve it by ordering the records by the id field.
$lastUserId = \App\Models\User::latest('id')->first();
echo $lastUserId;
Explanation:
This method sorts the users table in descending order by id and retrieves the first record. This will give you the most recently inserted record’s ID.
I hope this tutorial help you.