Getting roles that has the same exact permissions #1976
-
hello everyone, hope you are doing fine in laravel 8 , am using the spatie package for managing roles and permissions , and it's doing great I found that users can create roles with the same exact permissions (roles has many permissions) over and over again with different names for example , "Users Manager" would have these permissions :
however , the app admin can create another role "Boo Role" for example and give it the same exact permissions so my question is, how can i check if any existing role has the same exact given permissions by the user before creating a new one |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Get roles with a |
Beta Was this translation helpful? Give feedback.
-
this is the current solution that i got ; putting it here maybe it ll help some one $similar_role = Role::withCount('permissions')->whereHas('permissions', function ($query) use ($selectedPermissions) {
$query->whereIn('id', $selectedPermissions);
}, '=', count($selectedPermissions))->get(); then check if the count equals the selected new permissions to verify that the Role already exist $similar_role = $similar_role->filter(function ($item) use ($selectedPermissions) {
return $item->permissions_count === count($selectedPermissions);
}); |
Beta Was this translation helpful? Give feedback.
Get roles with a
whereHas
and in the whereHas add awhereIn
with the Permission ids, and add awithCount
for the exact number of Permission, and compare