Reputation: 459
I need to make duplicate of a column, but also I don't want to delete the old one. What would be the right way to copy value from one column into another? I have tried something like this, but it's not working:
Users::update([
'from_status' => 'status'
]);
I have to mention that, using MySQL Workbench
I have already added new column into table (it's column from_status
).
Also is it possible to update multiple columns at once this way? Column status
is not the only one I am trying to duplicate.
Upvotes: 0
Views: 734
Reputation: 1354
There are several ways to accomplish this task. And one of the simplest and the most straight way to do this is like following:
$users = User::all();
foreach($users as $user){
$user->from_status = $user->status;
$user->save();
}
Also is it possible to update multiple columns at once this way?
Yes. The same way by adding one more line for each column:
$users = User::all();
foreach($users as $user){
$user->from_status = $user->status;
$user->column2 = $user->some_column;
$user->save();
}
Upvotes: 1