Laravel 6 MySQL Get Last Inserted ID

Today, We want to share with you Laravel 6 MySQL Get Last Inserted ID.In this post we will show you How to Get the Unique ID for the Last Inserted Row, hear for Get Last Inserted ID in MySQL Table Using PHP Laravel 6 we will give you demo and example for implement.In this post, we will learn about How to get last inserted id in laravel PHP Framework? with an example.

Laravel 6 MySQL Get Last Inserted ID

There are the Following The simple About how to get last inserted id in laravel 6? Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel get last inserted id after save, so the get last updated id in laravel is used for this example is following below.

how to get last inserted id in laravel 6?

when We create database,We many a time use primary key column using the AUTO_INCREMENT attribute that means when We CRUD Operations insert Fresh row data into the mysql table after that value of primary key column will be auto-increment with unique integer.

Whenever We perform an INSERT or UPDATE on database table with an AUTO_INCREMENT column after that We fetch the last insert id of last insert or update query.

In Core PHP, We use mysqli_insert_id to retrive last inserted row id.

In laravel 6, when We go with DB query builder after that We can use insertGetId() that will insert a row after that return last inserted row id.

So in this small Web Post i will give you simple Laravel 6 three example for getting last insert id:

Example 1: Laravel 6.0 Fetch Last Inserted ID

using insertGetId

  public function getLastInsertedId()
    {
        $product_id = DB::table('products')->insertGetId(
                ['price' => '48500', 'name' => 'Mobile']
        );

        dump($product_id);
    }

Example 2: Retrive the Last Inserted Id Using Laravel 6 Eloquent

using Laravel 6 Model

Laravel 6 Retrive last id using save(), create() and insertGetId()

   public function getLastInsertedId()
    {
        $member = Member::create(['name'=>'RK' , 'last_name'=>'Rohit Sharma']);

        dump($member->id);
    }

Example 3: Retrieve last insert id by Eloquent

Using create() methods

Laravel 6 Retrive last id using save(), create() and insertGetId()

public function craeteNewMember()
{
    $members_id = DB::table('members')
    		->insertGetId(
            	['name' => 'Admin web', 'email'=>'[email protected]']
    		);
    return $members_id;

}
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel bulk insert get ids.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment