Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel DD helper未在每个函数内执行_Php_Laravel - Fatal编程技术网

Php Laravel DD helper未在每个函数内执行

Php Laravel DD helper未在每个函数内执行,php,laravel,Php,Laravel,我目前正试图通过复制具有适当关系的对象来解决问题。我通常使用Laravel的DD helper来查看是否获得了正确的信息,但在本例中,我不认为当它到达执行的方法中的行时,它正在运行 这是我处理复制的控制器方法 $copiedManagementSystem = $managementSystem->replicate(); $copiedManagementSystem->inspections->each(function($inspection) {

我目前正试图通过复制具有适当关系的对象来解决问题。我通常使用Laravel的DD helper来查看是否获得了正确的信息,但在本例中,我不认为当它到达执行的方法中的行时,它正在运行

这是我处理复制的控制器方法

        $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();
以下是管理系统与检查关系的模型

class ManagementSystem extends Model 
{
    protected $table = 'management_systems';

    protected $fillable = ['name', 'description'];

    public function inspections()
    {
        return $this->hasMany(Inspection::class);
    }
}
这是检验的模型与检验的关系

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);
    }
}
最后,这里是检查模型及其关系

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);
    }
}
我非常感谢您的帮助:)

编辑:所以我遇到了一个奇怪的情况。如果我运行以下命令:

dd($copiedManagementSystem->inspections->count();
它返回0。但如果我跑步:

dd($managementSystem->inspections->count());
它返回12,这是正确的值

有人知道为什么会这样吗?如果是,我如何解决这个问题

谢谢大家!

由于replicate()不复制关系,您可以尝试类似的方法

$copiedManagementSystem = $managementSystem->replicate()
foreach($managementSystem->inspections as $inspection) {
    $copiedManagementSystem->inspections->attach($inspection->replicate())
}
这是一个伪代码,如果您想链接原始检查,请删除$inspection->replicate()调用,只使用$inspection


$managementSystem可能具有的任何其他关系也是如此。

您对每个关系迭代的集合可能为空。请先检查一下。嗨,塔西亚祖马,藏品中肯定有一些物品,你有没有办法让我检查一下在第一个代码段的第二行尝试dd($copiedManagementSystem->inspections->count()),返回0。看来我得弄清楚为什么什么都没回来。谢谢你让我走上正轨!:)很高兴我们能帮助你!对我们的评论进行投票就好了:)