4 ways to Get Last Insert id in Laravel

July 5, 2022
Laravel
4 ways to Get Last Insert id in Laravel

In this guide, you will learn how to get the last insert id in laravel.

When we insert a record, we need that last insert id in order to insert another row as the foreign key in another table or need that id to the displayed at frontend.

We will discuss the solution to the problem by considering the example of Users and will share four different methods on how you can get the last insert id in laravel.

Lets Get Started!

Step 1: Install Laravel

The first step is to install the laravel itself, i am installing laravel in new folder of get_insert_id folder.

composer create-project --prefer-dist laravel/laravel get_insert_id

Step 2: Setup DB Credentials

Now go to the .env file and change DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD according to your database information.

Step 3: Migrate Database

Now you need to migrate the database by using running following command in the terminal:

php artisan migrate

Step 4: Create User Controller with index function

Create the User Controller by running the following command in terminal:

php artisan make:controller UserController

Then in the UserController itself, create the index function.

Your User Controller will look like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    //
    public function index(){
     
    }
}

Step 5: Create Route

Now we will create route for the UserController index function:

Route::get('/users',[\App\Http\Controllers\UserController::class,'index']);

Step 6: Add logic of get last insert id in the index function

Now in the index function, in which we created index function before in the UserController, we will create the logic of getting last insert id.

a) Get the last insert id By using create() method.

If you want to get the last insert id by create() method in laravel, here is how you do it:

$data = User::create(['name'=>'test','email'=>'test@test.com']);
dd($data->id);

//$data->id will give us last insert id of the user

The $data->id will give us the last insert id of the user.

b) Get the last insert id By Using the save() method.

The second way of getting the last insert id by save() method in laravel, here is how you do it:

$data = new User;
$data->name = 'test';
$data->email= 'test@test.com';
$data->save();
dd($data->id);

//$data->id will give us last insert id of the user

The $data->id will give us the last insert id of the user.

c) Get the last insert id By using getPdo() method.

Third way of getting the last insert id by using getPdo() method in laravel and here is how you do it:

DB::table('users')->insert([
    'name' => 'test',
    'email'=>'test@test.com'
]);
$id = DB::getPdo()->lastInsertId();
dd($id);

//$id will give us last insert id of the user

In the above code $id will give us the last insert id of the user

d) Get the last insert id By Using insertGetId() method.

The last and final way of getting the last insert id in laravel is by using insertGetId() method:

$id = DB::table('users')->insertGetId(
    [ 'name' => 'test', 'email'=>'test@test.com' ]
);

dd($id);
//$id will give us last insert id of the user

In the above code, the $id will give us the last insert id of the user.

Step 7: Run the application

Now if run the application by using the following command

php artisan serve

and in the browser type yourdomain.com/users, you will see the last insert of the user.

Conclusion

Hope you have learned today in the above article 4 different ways on how to get the last insert id in laravel.

See you in the next one!

Write a Reply or Comment

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


Icon