Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Copy record and duplicate with new values

Tags:

laravel

How can i copy a record and save it with a different value for 1 or more field?

for example: get----> first_name, last_name, civil_status, work

i want to copy the first name, and last name then insert it with a new civil status and work.

like image 435
Tupic Avatar asked Dec 13 '15 23:12

Tupic


People also ask

How do I copy records from one table to another in laravel?

We will follow the below steps: Create a new instance with replicate() method based on the old or original record. Store this new instance with save() method and setTable() method in the new table. Delete the old record from the original table with delete() method of a model.

How can we avoid inserting duplicate records in mysql using laravel?

Using insertOrIgnore we can easily remove duplicate content. So now see how to avoid duplicate entry in database in laravel application. So we will insert data laravel insert if not exists. We will ignore duplicate records while inserting records into DB table in laravel application using insertOrIgnore.


1 Answers

You could use the replicate method of model like this:

// Retrieve the first task $task = Task::first();  $newTask = $task->replicate(); $newTask->project_id = 16; // the new project_id $newTask->save(); 
like image 187
Pantelis Peslis Avatar answered Oct 13 '22 11:10

Pantelis Peslis