1

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.

Sujith Sandeep
  • 1,185
  • 6
  • 20
Cohchi
  • 543
  • 3
  • 9
  • 19
  • Does this answer your question? [Get the Last Inserted Id Using Laravel Eloquent](https://stackoverflow.com/questions/21084833/get-the-last-inserted-id-using-laravel-eloquent) – julianstark999 Jul 08 '22 at 09:35

2 Answers2

1

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;
0

Can do this:

    $id = $request->id;
    $data = Setting::find($id);
    // replicate
    $replicatedData = $data->replicate();
    // get convert the model collection to array
    $arrayReplicatedData $replicatedData->toArray();
    //save it with the create method
    $newCreatedModel = Setting::create($arrayReplicatedData);
    // Get the new id
    $newId = $newCreatedModel->id;