Laravel 5 Get Last Inserted ID

Laravel 5 Get Last Inserted ID

By : Laravellogic Created : October 21, 2019 View : 10

Hello, In this tutorial we will mack get last inster id in laracel web applications. Are you not know how to get last instert id in laravel? if yes this is right place.  Because in this post we have share Laravel tutorial on getting last inserted data id by 3 different method

many developer find how to get last insert id in laravel applications, Because all most applications need last insert id, so we are get last insert id in 3 different method

(1)insertGetId()

(2)create(),

(3)save()

method :1 insertGetId()

public function craete(){

    $input['first_name'] = 'vikram';
    $input['last_name'] = 'vanaliya';
    $input['email'] = 'vikraminphp@gmail.com';

    $id = DB::table('users')->insertGetId($input);

    print_r($id);
}

method :2 create()

public function craete(){

    $input['first_name'] = 'vikram';
    $input['last_name'] = 'vanaliya';
    $input['email'] = 'vikraminphp@gmail.com';

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

    print_r($user->id);
}

method :3 save()

public function craeteNewUser(){

    $user = new User();

    $user->first_name = 'vikram';
    $user->last_name = 'vanaliya';
    $user->email = 'vikraminphp@gmail.com';

    $user->save();

    print_r($user->id);

}

I hope in this tutorial you helpful.