Laravel 8 Get Last ID of an Inserted Model

Laravel Get Last Id of an Inserted Model

In this post, I want to share a quick tip about how to get the last inserted ID in Laravel. While working on your Laravel application, you often need to get the recently added model ID to assign it to some other model or processing data. You can achieve this in different ways and also depends you are using the DB facade of Laravel or using a model class.

So without any delay, let’s see how we can get the last inserted ID of a model/table in Laravel.

1. Using Eloquent Save Method

One of the easiest and most efficient ways to grab the last inserted ID for a model is to use the save() method when you create/update an eloquent model.

$product = new Product();
$product->name = "Acer Aspire 5 Slim Laptop";
$product->save();

$productId = $product->id();

dd($productId); // will spit out product id

In the above example, we created a new product using the Product model, and right after saving the model we assigned the id of the last saved product to $productId variable.

2. Using Elogquent Create Method

This method is the same as the previous one, except we create the model entry using create() method. In this method, we will create a new product using the create method, and right after the creation, we can grab the id of the model.

$product = Product::create(['name' => 'Acer Aspire 5 Slim Laptop']);
$productId = $product->id();

dd($productId); // will spit out product id

3. Using DB Facade

You can also create the model/table entry using the DB facade and get the ID right at the same time. For this purpose, we can call the insertGetId() method to insert and return the id of the table row.

$productId = DB::table('products')->insertGetId(
    [ 'name' => 'Acer Aspire 5 Slim Laptop' ]
);

dd($productId); // will spit out product id

4. Using getPDO() Method

You can also use the PDO Object to get the last inserted id of a table in Laravel. Firstly you will have to insert the record and right after that, you can call the lastInsertId() method on the PDO object.

$product = DB::table('users')->insert(
    [ 'name' => 'Acer Aspire 5 Slim Laptop' ]
); 
$productId = DB::getPdo()->lastInsertId();.

dd($productId); // will spit out product id

That’s all for now. I hope this small tip will help you but I guess most of you will know these or some of these methods already if you are in Laravel development for some time. If you have any questions or comments please let us know in the comments box below.

5 comments on “Laravel 8 Get Last ID of an Inserted Model

  1. Hi thank you for all series and for helping us to learn
    I followed your laravel-ecommerce-application and I get some problem and i don’t know why this happen and how to solve that so can I show you my code and help me to find a solution if you aggre and you have time to help me I will tell you what’s the problems and you check the code and thank you very much

Leave a Reply

Your email address will not be published. Required fields are marked *

*

When sharing a code snippet please wrap you code with pre tag and add a class code-block to it like below.
<pre class="code-block">you code here</pre>

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your little help will keep this site alive and help us to produce quality content for you.