Laravel 5.6 - Get last inserted id using save(), create() and insertGetId()

If you have experience with PHP Project developing then you know we almost require to get last id. so if you need to get last insert id then you can get save(), create() and insertGetId() method in laravel 5.6 application. here i will give you three way to get last inserted id or object in laravel 5.6 application.

So in this small article i will give you three example for getting last insert id:

1) save()

2) create()

3) insertGetId()

So you can see following craeteNewUser() method with following three way to get and return last inserted id. you can use any one that is fit for your example. So let's see bellow examples:

Example 1: Using save()

public function craeteNewUser(){

$user_data =new User();

$user_data->name = "Akash Savani";

$user_data->email = "";


return $user->id;


Example 2: Using create()

public function craeteNewUser()


$id = DB::table('users')


['name' => 'Akash Savani', 'email'=>'']


return $id;


Example 3: Using insertGetId()

public function craeteNewUser()


$input = ['name' => 'Akash Savani', 'email'=>''];

$user = User::create($input);

return $user->id;


I hope you found your best solution....

March 13, 2018 | 1527 Viewer | Category : PHPLaravelLaravel 5Laravel 5.6
Related Posts