Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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.

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 :

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.   Laravel 6 Eloquent Has With and WhereHas
  2.   How to Get Laravel last inserted ID from database
  3.   jquery validate select dropdown Example
  4.   Laravel 6 get last Insert id from query builder
  5.   Laravel Complex queries using Eloquent Query
  6.   Laravel Eloquent Get Last Inserted Id
  7.   Get last inserted ID laravel 5.7 Examples
  8.   how to run sql query in command prompt?
  9.   Laravel 6 Where null and Where not null eloquent query
  10.   Laravel 6 Update Query using Eloquent Example