Get the Last Inserted Id Using Laravel Eloquent

Posted by LaravelIndia - 4 months ago

Using create() method.

$data = Model::create(['name'=>'first']);
dd($data->id);

Using save() method.

$data = new Model;
$data->name = 'Test';
$data->save();
dd($data->id);

Laravel : Get last insert ID using Eloquent

$user           = new Model(User);
$user->name     = "John";
$user->age      = "28";
$user->save();

//Get last inserted id
$lastInsertedId = $user->id