when using larave"s gate for permission verification
Admin_users (user table) Admin_roles (role table) Admin_permissions (permissions table) are many-to-many relationships
first, register gate in App/providers/AuthServiceProvider
$permissions=AdminPermission::all();
foreach ($permissions as $permission){
//$AdminUser
Gate::define($permission->name,function($AdminUser) use ($permission){
return $AdminUser->hasPermission($permission);
});
}
hasPermission () code in AdminUser.php
Thecode indicates that relRole is the model association defined in AdminUser, the association to the Admin_ roses table
relPermission is the model association defined in AdminRole, and the association to the Admin_ permissions table
this code is a nested association of the model in order to obtain the permissions owned by the current user
//
public function hasPermission($permission){
//
$allPermission=$this->with(["relRole.relPermission"])->find($this->id);
dd($allPermission);
return !! $allPermission->contains($permission);
}
current problem:
I myself want to compare the full permissions of the user with the current permissions, but now there are two problems
1. Permissions are scattered under different role data.
2. When judging with the contains () method of a set, the prompt function does not exist because it is not a set itself.
attach the acquired $allPermision data
according to the user getting all the permissions of the user, and judging whether the current permission is in the user"s authority, this business logic, how should I modify my code? thank you for correcting