How to get last insert id in Eloquent ORM laravel

97,000

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.

Share:
97,000

Related videos on Youtube

Deenadhayalan Manoharan
Author by

Deenadhayalan Manoharan

Top-users-from-coimbatore

Updated on January 03, 2020

Comments

  • Deenadhayalan Manoharan
    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?

  • Raham
    Raham about 8 years
    Dear @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
    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
    Raham about 8 years
    actually 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
    Majbah Habib over 7 years
    $lastInsertedID = DB::table('users') ->insert( array( 'email_id' => $email_id, 'name' => $name ) ) ->lastInsertId();
  • Yasin Yörük
    Yasin Yörük over 7 years
    Thanks 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
    Majbah Habib over 7 years
    Thanks for your response
  • Wesley Smith
    Wesley Smith over 7 years
    It's worth noting that the doc is misleading because the first method shown is ->save(); and it doesnt, IMO, do enough to explain that save() returns a boolean while only Insert, update, and delete return the instance. Especially since in says "After saving or creating"
  • Captain Hypertext
    Captain Hypertext over 7 years
    It's worth noting that insertGetId only works if the column is actually named id.
  • SaidbakR
    SaidbakR about 7 years
    The problem is the trigger generated keys