检索Laravel 5 /委托中具有特定权限的所有用户


Retrieving all users of a specific permission in Laravel 5 / Entrust

如何在委托中检索具有特定权限的用户

use App'Role;
use App'Permission;    
$permission = Permission::find(1);
$roles = $permission->roles;
$users = $roles->users;

我知道代码不工作…举个例子

try this

$users = User::permission('edit articles')->get(); // Returns only users with the permission 'edit articles'

我以前从未使用过委托,但我在GitHub上查看了代码。

Permission有一个角色的belongsToMany

Role有一个用户的belongsToMany

因此,您应该能够像这样链接命令:

$permission = Permission::find(1);
$users = $permission->roles()->users;

如果您打算链接关系,您应该在关系名称上添加大括号。

多亏了ntzm,我才知道如何找到所有有权访问权限的用户。不确定这对谁有帮助。

有两种不同类型的员工具有访问权限。一个来自角色,第二个是在哪里给他们许可。我还没有使用direct,但是我确实有可以访问权限的角色。下面是我如何获得具有特定权限的用户:

    $permission = Permission::findByName('edit posts');
    $employees = [];
    //Loop through each role.
    foreach($permission->roles as $r){
        $emps = $r->users;
        //Cycle through the users.
        foreach($emps as $e){
            $employees[] = $e;
        }
    }
    return $employees;

希望这能帮助到需要帮助的人。:)