Laravel 复制记录并使用新值复制但不使用唯一字段
Laravel copy record and duplicate with new values But not with unique field
我想用新值复制我的记录。
我正在使用这些代码,它们与唯一字段 (id) 配合得很好。
$task = tableName::find($request->id);
$newTask = $task->replicate();
$newTask->newField= 2;
$newTask->save();
我的 table 看起来像:
id
no
first
second
newField
1
45
1000
650
1
2
52
4500
45
1
3
45
1250
75
1
4
58
7500
86
1
5
45
250
72
1
我想用新值复制所有行。 只是newField必须是2.
您需要针对特定 no
获取所有记录。尝试这样的事情
$tasks = tableName::where('no',$request->id)->where('newField',1)->get();
foreach($tasks as $task)
{
$newTask = $task->replicate();
$newTask->newField= 2;
$newTask->save();
}
请同时查看官方documentation。
我想用新值复制我的记录。
我正在使用这些代码,它们与唯一字段 (id) 配合得很好。
$task = tableName::find($request->id);
$newTask = $task->replicate();
$newTask->newField= 2;
$newTask->save();
我的 table 看起来像:
id | no | first | second | newField |
---|---|---|---|---|
1 | 45 | 1000 | 650 | 1 |
2 | 52 | 4500 | 45 | 1 |
3 | 45 | 1250 | 75 | 1 |
4 | 58 | 7500 | 86 | 1 |
5 | 45 | 250 | 72 | 1 |
我想用新值复制所有行。 只是newField必须是2.
您需要针对特定 no
获取所有记录。尝试这样的事情
$tasks = tableName::where('no',$request->id)->where('newField',1)->get();
foreach($tasks as $task)
{
$newTask = $task->replicate();
$newTask->newField= 2;
$newTask->save();
}
请同时查看官方documentation。