laravel get last insert id code example

Example 1: how to get last inserted id in laravel

// how to get last inserted id in laravel

//METHOD-1
$data->save();
$lastId = $data->id;

//METHOD-2
$data = Booking::latest('id')->first();
$id = data->id;

Example 2: return last inserted id in laravel

$id = DB::table('users')->insertGetId([
    'email' => '[email protected]',
    'votes' => 0
]);

Example 3: Get last id in laravel

$last3 = DB::table('items')->latest('id')->first();

Example 4: laravel query builder get last insert id

$id = DB::table('users')->insertGetId(
    ['email' => '[email protected]', 'votes' => 0]
);

Example 5: laravel db insert get last id

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

  ->insertGetId(

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

);

Example 6: laravel get last id

DB::table('myTable')->orderBy('id','desc')->first();

Tags:

Php Example