[英]Filtering pivot table data using intermediate relationship in Laravel eloquent
I'm working on Access Control Level (ACL) in laravel.我正在 Laravel 中研究访问控制级别 (ACL)。 here is, two pivot
table one is role_user
which is represent the each users roles
and another one is permission_role
which is represent the each roles permissions
.这里有两个pivot
表,一个是role_user
代表each users roles
,另一个是permission_role
代表each roles permissions
。 now I want to get the permission_role
pivot table data from User
model.现在我想从User
模型中获取permission_role
数据透视表数据。
my code samples.我的代码示例。
In User Model
public function roles()
{
return $this->belongsToMany(Role::class);
}
In Role Model
public function users()
{
return $this->belongsToMany(User::class);
}
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
In Permission Model
public function roles()
{
return $this->belongsToMany(Role::class);
}
I'm try this way:-我正在尝试这种方式:-
$user->roles->with('permissions')->get();
it shows BadMethodException.它显示了 BadMethodException。
How could i solve this issue and get the desire output.我怎样才能解决这个问题并获得所需的输出。
If you want to eager load nested relationships you can do by using the dot separated syntax eg:如果您想预先加载嵌套关系,您可以使用点分隔语法,例如:
User::with('roles.permissions')->find($id);
If the User
model has already been instantiated then you can use the load()
method instead (lazy eager loading):如果User
模型已经被实例化,那么你可以使用load()
方法代替(延迟预加载):
$user->load('roles.permissions');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.