Get the Last Inserted ID Laravel
Get Laravel Insert Statement ID Row. Simple 4 Methods to follow.
These are short code snippets on how to get your last inserted ID on Laravel.
1) Using the insertGetId() method
$id = DB::table('users')->insertGetId(
[ 'name' => 'first' ]
);
dd($id);
2) Using the lastInsertId()
DB::table('users')->insert([
'name' => 'TestName'
]);
$id = DB::getPdo()->lastInsertId();;
dd($id);
3) Using create() method
$data = User::create(['name'=>'first']); dd($data->id);
4) Using save() method
$data = new User; $data->name = 'Test'; $data->save(); dd($data->id);