Laravel : Get last insert ID using Eloquent

Vineeth Vijayan
Vineeth Vijayan
Published in
1 min readSep 10, 2017

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.

Happy coding!!

Love this article? Click here to buy me a coffee :)

--

--

Vineeth Vijayan
Vineeth Vijayan

Software Engineer | Scribbler | Loves coding and movies | Passive Reader | Lazy Blogger