Laravel DD helper不在每个函数中执行 [英] Laravel DD helper not executing inside each function
问题描述
我目前正在尝试通过复制具有适当关系的对象来解决问题.我通常使用Laravel的DD帮助程序来查看是否获得了正确的信息,但是在这种情况下,当它碰到要执行的方法中的行时,我认为它并没有在运行.
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);
}
}
非常感谢您的帮助:)
因此,我遇到了一个奇怪的情况.如果我运行以下命令:
So I've come across a strange occurrence. If I run the following:
dd($copiedManagementSystem->inspections->count();
它返回0.但是如果我运行:
It returns 0. But if I run:
dd($managementSystem->inspections->count());
它返回12,这是正确的值.
It returns 12, which is the correct value.
有人知道为什么会这样吗?如果是这样,我该如何解决该问题?
Does anyone know why this happens? And if so, how can I fix the issue?
谢谢!
推荐答案
由于copy()不会复制关系,因此您可以尝试执行以下操作.
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())
}
这是伪代码,如果您希望链接原始检查,请删除$ inspection-> replicate()调用,而只需使用$ inspection
It is pseudo code, if you want the original inspections linked remove the $inspection->replicate() call and just use $inspection
$ managementSystem可能具有的任何其他关系也是如此.
Same goes for any other relations $managementSystem might have.
这篇关于Laravel DD helper不在每个函数中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!