I'm currently trying to troubleshoot my way through duplicating an object with its appropriate relationships. I usually use Laravel's DD helper to see if I'm getting the right information, but in this instance, I don't think that it's being run when it hits the line in the method that gets executed.
Here's my controller method that's handling the duplication.
$copiedManagementSystem = $managementSystem->replicate();
$copiedManagementSystem->inspections->each(function($inspection) {
$copiedInspection = $copiedManagementSystem->inspections()->create([
'name' => $inspection->name,
'management_system_id' => $inspection->managementSystemId,
'pass_score' => $inspection->passScore,
'max_score' => $inspection->maxScore,
'order' => $inspection->order,
]);
dd($inspection); //I've placed the dd here but it doesn't work in any position, in any part of the each function.
$inspection->checks->each(function($check){
$copiedInspection->checks()->create([
'question' => $check->question,
'values' => $check->values,
'type' => $check->type,
'inspection_id' => $check->inspectionId,
'order' => $check->order,
]);
});
});
$copiedManagementSystem->save();
Here is the ManagementSystem's model with the inspections relationship
class ManagementSystem extends Model
{
protected $table = 'management_systems';
protected $fillable = ['name', 'description'];
public function inspections()
{
return $this->hasMany(Inspection::class);
}
}
This is the inspection's model with the relations
class Inspection extends Model
{
protected $table = 'inspections';
protected $casts = [
'order' => 'integer'
];
protected $fillable = [
'name',
'management_system_id',
'pass_score',
'max_score',
'order'
];
public function checks()
{
return $this->hasMany(Check::class);
}
public function managementSystem()
{
return $this->belongsTo(ManagementSystem::class);
}
}
And finally, here is the check model with its relations.
class Check extends Model
{
protected $table = 'checks';
protected $fillable = [
'question',
'values',
'type',
'inspection_id',
'order'
];
public function inspection()
{
return $this->belongsTo(Inspection::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
}
I'd really appreciate any help :)
EDIT: So I've come across a strange occurrence. If I run the following:
dd($copiedManagementSystem->inspections->count();
It returns 0. But if I run:
dd($managementSystem->inspections->count());
It returns 12, which is the correct value.
Does anyone know why this happens? And if so, how can I fix the issue?
Thank you!
Since replicate() does not replicate the relations you could try something like this.
$copiedManagementSystem = $managementSystem->replicate()
foreach($managementSystem->inspections as $inspection) {
$copiedManagementSystem->inspections->attach($inspection->replicate())
}
It is pseudo code, if you want the original inspections linked remove the $inspection->replicate() call and just use $inspection
Same goes for any other relations $managementSystem might have.
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.