Home » PHP Laravel Get Last Inserted ID in MySQL Table

PHP Laravel Get Last Inserted ID in MySQL Table

Today, We want to share with you PHP Laravel Get Last Inserted ID in MySQL Table.In this post we will show you Laravel Get Last Insert ID using Eloquen, hear for Laravel 5.5 & 5.6 – Get Last Inserted ID With Example we will give you demo and example for implement.In this post, we will learn about How to get last inserted id in Laravel 5.5 and 5.6 Example with an example.

PHP Laravel Get Last Inserted ID in MySQL Table

There are the Following The simple About PHP Laravel Get Last Inserted ID in MySQL Table Full Information With Example and source code.

Read Also:  How To Configure Supervisor For Laravel 5/6/7 Queue Jobs?

As I will cover this Post with live Working example to develop Laravel 5.5, 5.6 – Get last inserted id using save(), create() and insertGetId(), so the How to get last inserted id in laravel PHP Framework for this example is following below.

  • using save()
  • using create()
  • using insertGetId()

Laravel 5.6 – Get last inserted id

Example 1: Using Laravel 5.6 save()

using Laravel 5.6 SAVE() Methods – last insert id in laravel 5.6

public function craeteNewMember(){
  $member_data =new Member();
  $member_data->name = "Jaydeep Gondaliya";
  $member_data->email = "[email protected]";
  $member_data->save();
  return $member->id;
}

Example 2: Using create()

Simple create() Laravel 5.6 Methods(laravel 5.6 eloquent last insert id)

public function craeteNewMember()
{
    $id = DB::table('members')
    		->insertGetId(
            	['name' => 'Jaydeep Gondaliya', 'email'=>'[email protected]']
    		);
    return $id;
}

Example 3: Using insertGetId()

using Laravel 5.6 insertGetId() function(laravel 5.6 db::table insert get id)

public function craeteNewMember()
{
	$input = ['name' => 'Jaydeep Gondaliya', 'email'=>'[email protected]'];
    $member = Member::create($input);
    return $member->id;
}

Laravel 5.5 – Get Last Inserted ID With Example

Example 1 : 1) Using insertGetId() method

Laravel 5.6 get last inserted ID using function insertGetId()

$members_id = DB::table('members')->insertGetId(
    [ 'name' => 'pakainfo_jaydeep' ]
);

dd($members_id);

Example 2) Using Laravel lastInsertId() method

lastInsertId() in laravel(last inserted id in laravel)

DB::table('members')->insert([
    'name' => 'pakainfo'
]);
$lasr_id = DB::getPdo()->lastInsertId();;
dd($lasr_id);

Example 3) Using create() Laravel 5.5 method.

laravel create() method with laravel Model(get last inserted id)

$members = Member::create(['name'=>'jaydeep']);
dd($members->id);

Example 4) Using Laravel save() method.

get last inserted ID using laravel 5.5 save() method

$members = new Member;
$members->name = 'Pakainfo.com';
$members->save();
dd($members->id);

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Laravel 5.5 - Get Last Inserted ID With Example

Summary

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

I hope you get an idea about PHP Laravel Get Last Inserted ID in MySQL Table.
I would like to have feedback on my Pakainfo.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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Retrieve last insert id by Eloquent in Laravel 6
  2. Read Also:  Linear Search in C, C++, JAVA, PHP, Python3 and C#
  3. Read Also:  Hello World with ASP.NET Core - Install ASP.NET Core steps
  4. Read Also:  Simple Print AngularJS Hello World program From scratch
  5. Read Also:  How to Get Laravel last inserted ID from database
  6. Read Also:  Get the Last Inserted Id Using Laravel Eloquent
  7. Read Also:  how to get last inserted id in laravel 6
  8. Read Also:  Bash if else Statment
  9. Read Also:  Laravel 5.6 Admin Panel Example Tutorial From Scratch
  10. Read Also:  PHP wp_get_attachment_image_url wordpress function

Leave a Comment

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