I want to replicate multiple relational rows to the same table with the diff job id. But it doesn't work.
Here is my code
$parentJobUnits = Unit::where('job_id',$jobId)->get(); //may be single or multiple rows for units.
$JobCopy = $job->replicate()->save(); //main job copied for new units.
$partsCopy = array_map(function(Unit $unit) use($JobCopy)
{
$newUnit = $unit->replicate();
$newUnit->job_id = $JobCopy->id;
return $newUnit->save();
}, $parentJobUnits);
The above code is not working for multiple row replication.
Try removing the save
method which causes a true return and not the copied record
correct: $job->replicate();
incorrect: $job->replicate()->save();
otherwise I'd do something like...
$parentJobUnits = Unit::where('job_id',$jobId)->get();
foreach($parentJobUnits as $unit)
{
$unit->replicate()->save();
}
replicate()
function does not work on get()
function. It only works with find()
function.
$parentJobUnits = Unit::where('job_id',$jobId)->get();
foreach($parentJobUnits as $key => $value)
{
$new_unit = Unit::find($value->id)->replicate(); // $value->id is your primary key
$new_unit->save();
}
It will replicate the multiple rows.
$CollageData= Collage::where('student_id',$student_id)->get();
foreach($CollageData as $key => $collage )
{
$collage->replicate()->save();
}
This Works for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.