Laravel 5.5 – Get Last Inserted ID With Example

Today, I am share with you how to get last inserted ID in laravel with 5 Eloquent simple example. i have needed some it last insert ID in laravel web application. therefor, here i have find 4 diffrent methods to get last inserted ID in laravel apps. you can use it and get last ID of inserted record in laravel application.

How to Get last insert ID using Laravel
How to Get last insert ID using Laravel

1) Using insertGetId() method

You can get last inserted ID using insertGetId() in laravel for example

$id = DB::table('users')->insertGetId(
    [ 'name' => '4cgandhi' ]


2) Using lastInsertId() method

You can also get last inserted ID using lastInsertId() in laravel. You need PDO object to run this method for example

    'name' => 'TestName'
$id = DB::getPdo()->lastInsertId();;


3) Using create() method.

Yes, you can also get last ID using of laravel create() method with laravel Model. this methos is return last inssert recod’s object and you can get using this object last insert ID. for example

$data = User::create(['name'=>'4cgandhi']);

4) Using save() method.

You can also get last insert ID using laravel save() method. if you use save() method for insert data then you can get last inserted id like that

$data = new User;
$data->name = 'Test';

Also Read: Laravel 6.2 Get Last insert ID

I hope you get an idea about Get the Last insert Id Using Laravel Eloquent.
I would like to have feedback on my 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.

We are hope this small web tutorials help everyone. if you known more way to get last insert ID in laravel so please comment bellow. Thanks..

Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel Eloquent Get Last Inserted Id
  2.   how to get last inserted id in laravel 6?
  3.   PHP Laravel Set Active Menu Examples
  4.   Redirecting a Web Folder Directory to another Directory in htaccess
  5.   Laravel MySQL Database Stored Procedure with pagination Eloquent ORM
  6.   Email Verification in Laravel 5.7 Example Tutorial
  7.   updated_option Wordpress hook details - WORDPRESS ACTION HOOKS
  8.   PHP Laravel Get Last Inserted ID in MySQL Table
  9.   Laravel 6 insert query Example
  10.   AngularJS Login Script using PHP MySQL