Laravel 8 how to replicate and get id of new model

Viewed 139

I'm using laravel and I would like to replicate some data. So I did that :

$id = $request->id;
$data = Setting::find($id);
$newSetting = $data->replicate()->save();

But how to get the id of $newSetting ? to save that return's only true and I need to return my new id. I tried with push in place of save but that also return's true. I checked the documentation but I couldn't find it.

Can somebody help me please ?

Thanks in advance.

1 Answers

you just have to :

    $id = $request->id;
    $data = Setting::find($id);
    // now replicate data and save
     $newData = $data->replicate();
   //save the new data
     $newData->save();
     //get the new id
    $newId = $newData->id;
Related