Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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 雄辩的多对多对多-如何轻松加载远程关系_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 雄辩的多对多对多-如何轻松加载远程关系

Php 雄辩的多对多对多-如何轻松加载远程关系,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有三张桌子;用户、组和权限 在模型中,我将关系设置为belongtomany 在用户模型中: public function groups() { return $this->belongsToMany('Group'); } 分组模式: public function users() { return $this->belongsToMany('User'); } public function permissions() { return $this

我有三张桌子;用户、组和权限

在模型中,我将关系设置为belongtomany 在用户模型中:

public function groups() {
    return $this->belongsToMany('Group');
}
分组模式:

public function users() {
    return $this->belongsToMany('User');
}

public function permissions() {
    return $this->belongsToMany('Permission');
}
在权限模型中:

public function groups() {
    return $this->belongsToMany('Group', 'id');
}
多用户对多组 多组对多权限


我正在尝试获取用户拥有的所有权限,但不知道它的代码应该是什么样子。有人能帮忙吗?

如果你急于加载,它应该是这样的

$user = User::where('id', $id)->with(['groups.permissions'])->first();

这就是你可以做到的:

User::where('id', $id)->with(['groups.permissions' => function ($q) use (&$permissions) {
     $permissions = $q->get()->unique();
}])->first();

// then
$permissions; // collection of unique permissions of the user with id = $id

我如何获得许可$q=User::where('id',$id)->with(['groups.permissions'])->first();dd($q->许可);没有显示任何内容
$q->groups[$index]->permissions
我试图避免循环,比如查询获取数组或集合中的所有权限,但不知道如何操作,但它确实有效。需要深入阅读文档。超级聪明我的friend@PawelBieszczad你在文件里找不到。这只是为了得到你需要的东西。缺点是,它将运行一个额外的查询来获取它,但这比在相关集合中循环要容易得多,而且它甚至可以处理更深层的嵌套关系。我有一个类似的问题,但我有点卡住了,你能帮我一点忙吗,Jarek@dbr这应该可以