简体   繁体   English

Spatie Laravel 用户角色权限

[英]Spatie Laravel user role permission

I am new to Laravel.我是 Laravel 的新手。 I have implemented a search with user role and permission.我已经实现了具有用户角色和权限的搜索。 If I selected a role it should display all the users assigned to that role...same way for permission and if I search for the particular user it should display the user details with role and permission.如果我选择了一个角色,它应该显示分配给该角色的所有用户......相同的权限方式,如果我搜索特定用户,它应该显示用户详细信息以及角色和权限。

User with particular role具有特定角色的用户

User::role('admin')->get()

User with particular permission具有特定权限的用户

User::permission('view')->get();

User with name有名字的用户

User::with('roles.permissions')->with('permissions')->where('name' , 'testadmin')->first();

I wanted to know is this correct?我想知道这是正确的吗? Thanks in Advance!提前致谢!

You can get all the users who has roles/Permission by using wherehas method您可以使用wherehas方法获取所有具有角色/权限的用户

To Get all the User with Admin Role获取所有具有管理员角色的用户

User::with('roles')
   ->whereHas('roles',function($query){
        return $query->whereIn('name',['Admin']);
   });

To get users who have view permission获取有查看权限的用户

User::with(['permission'])
    ->whereHas('permission',function($query){
        return $query->whereIn('name',['view']);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM