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
急切加载中的实例模型已为空laravel_Laravel - Fatal编程技术网

急切加载中的实例模型已为空laravel

急切加载中的实例模型已为空laravel,laravel,Laravel,我通过以下方式获得菜单和菜单: Menu::with('getChilds')->where(['group' => 1])->get(); 在菜单模式中: public function getChilds() { return $this->hasMany('App\Models\Menu', 'parent', 'id')->where(['group' => $this->group])->with('getChilds'); }

我通过以下方式获得菜单和菜单:

Menu::with('getChilds')->where(['group' => 1])->get();
在菜单模式中:

public function getChilds()
{
    return $this->hasMany('App\Models\Menu', 'parent', 'id')->where(['group' => $this->group])->with('getChilds');
}

但是为什么$this->group返回空值呢?。请帮帮我。谢谢。

您不需要在模型查询中传递

您需要自定义您的雄辩的查询

Menu::with(['getChilds' => function ($query) {
        $query->where('group',1);
}])->get();
在菜单模式中

public function getChilds()
{
    return $this->hasMany('App\Models\Menu', 'parent', 'id');
}

正如您在模型中看到的,我使用('getChilds')回调以获得多个子菜单级别,这就是为什么我必须添加条件以获得深度不受限制的子菜单,您的X只提供1个子菜单级别。我的问题是为什么$this->group return null,我不能在这种情况下使用它,对吗?。