Advertisement — Top

Responsive Advertisement

Laravel : Get last insert ID using Eloquent

Eloquent ORM is a simple active record implementation in Laravel for interacting with the database.
With Eloquent ORM, create a model instance, let`s say $user = new User. Then set attributes on this model, like name, age, phone etc. Finish with save() method. By inserting data like this into the database, above initialized object say $user can be user to get last inserted ID by $user->id. A complete insertion code would like this.
$user           = new User;
$user->name = "Tom";
$user->age = "29";
$user->save();
//Get last inserted id
$lastInsertedId = $user->id
That`s it. There are other options in Larvel query builder as well. Try both and choose the right one for your requirements.

Post a Comment

0 Comments