How to get last insert id in Eloquent ORM laravel
Solution 1
Like the docs say: Insert, update, delete
"You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.
After saving or creating a new model that uses auto-incrementing IDs, you may retrieve the ID by accessing the object's id attribute:"
$insertedId = $user->id;
So in your sample:
$user = User::create($loginuserdata);
$insertedId = $user->id;
then on table2 it is going to be
$input['table2_id'] = $insertedId;
table2::create($input);
Solution 2
**** For Laravel ****
$user = new User();
$user->name = 'John';
$user->save();
//Getting Last inserted id
$insertedId = $user->id;
Solution 3
You could wrap your data in the insertGetId() method like this
$id = DB::table('table')->insertGetId( $data );
In this case the array $data would look something like this
$data = [ 'field' => 'data' , 'field' => 'data' ];
and if you’re using the DB façade you could just append the lastInsertID() method to your query.
$lastInsertedID = DB::table('table')->insert( $data )->lastInsertId();
Solution 4
$lastId = User::create($loginuserdata)->id;
Solution 5
You may do it as,
public function store(Request $request,ModelName $obj)
{
$lastInsertedId = $obj->create($request->all())->id;
}
Hope this will help you.
Related videos on Youtube
Comments
-
Deenadhayalan Manoharan over 4 years
I am performing a database operation with "Eloquent ORM in Laravel". I just want to take the last insert id (not the maximum id) in the database.
I searched to get last insert id in laravel Eloquent ORM, I got following link (Laravel, get last insert id using Eloquent) that is refer to get last insert id from following function "
$data->save()
".But I need to get
"
Model::create($data)
";My Query:
GeneralSettingModel::create($GeneralData); User::create($loginuserdata);
How can I retrieve the last inserted id?
-
Luís Cruz over 8 yearsPossible duplicate of Laravel, get last insert id using Eloquent
-
Niklesh Raut over 6 yearsIf
If the table has an auto-incrementing id
Try this : stackoverflow.com/a/46482971/2815635
-
-
Raham about 8 yearsDear @Krzysiek Grzembski I need it in my controller.I changed it in the model as you written it in the model.But now $model->userid; is where accessible.I need it in my controller.Please guide...
-
Krzysiek Grzembski about 8 years@Raham Please see my updated answear. It is hard to tell what is your problem without any piece of code.
-
Raham about 8 yearsactually I solved my problem.My problem was that how can I retrieve the last inserted one from table.Now I looked up for your answer that how you return the last inserted one.where you defined $model...
-
Majbah Habib over 7 years$lastInsertedID = DB::table('users') ->insert( array( 'email_id' => $email_id, 'name' => $name ) ) ->lastInsertId();
-
Yasin Yörük over 7 yearsThanks for answer but I solved my issue. It's about primaryKey. I was changed primarkey in Model. So I didnt get last id. After change primarykey to id, it worked.
-
Majbah Habib over 7 yearsThanks for your response
-
Wesley Smith over 7 yearsIt's worth noting that the doc is misleading because the first method shown is
->save();
and it doesnt, IMO, do enough to explain thatsave()
returns a boolean while only Insert, update, and delete return the instance. Especially since in says "After saving or creating" -
Captain Hypertext over 7 yearsIt's worth noting that
insertGetId
only works if the column is actually namedid
. -
SaidbakR about 7 yearsThe problem is the trigger generated keys