Laravel Copy Record and Duplicate with New Values

How can I easily copy the database row and paste it again, but with a different value for 1 field?

for example: get ----> taks_name, project_id I want to copy the task and paste it with the new project_id

+6
source share
3 answers

You can use the replicate model method as follows:

 // Retrieve the first task $task = Task::first(); $newTask = $task->replicate(); $newTask->project_id = 16; // the new project_id $newTask->save(); 
+19
source

You can use the replicate method. This will create a new object with the same values ​​except the primary key and timestamps. After that you can save your model:

 $task = Task::find(1); $new = $task->replicate(); 

If you want, you can change the property

 $new->project = $otherProject; 

and then

 $new->save(); 

if you want a new id just:

 $newID = $new->id; 
+7
source

You can use replicate() and then update this line:

 Model::find(1)->replicate()->save(); $model = Model::find(1); $model->project_id = $new_project_id; $model->save(); 
+1
source

All Articles