Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

php - laravel update row to be a copy of another already existing row

I am trying to copy all data from one row to another already existing row and change some values. For example

  1. if the user wants to update row#1, all updates are saved in addition it creates a new row with the current date and new id like row#2. $updatedEmp = $emp->replicate();

  2. if the user wants to update the same row#1 again, all changes are saved but this time row#2 does already exist. I want to copy all changes from row#1 to row#2 but with the same id of row#2. I've tried to use replicate() function, but it creates a new row and I get an error duplicate for primary key.

I've tried to change the id, but it did not work

if($query){ //not empty
 $updatedEmp = $emp->replicate();
 $updatedEmp->id = $query->id;
 $query = $updatedEmp;
 $query->save();
}

I don't have problems with step 1 but step 2 is not working for me. How can I make laravel to update the row instead of inserting a new one?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...