如何在 Laravel5.2 中找到当前用户的角色


How to find current user's role in Laravel5.2?

我在 Laravel 5.2,
中使用包"romanbican/roles"如何找到当前用户的角色?

控制器:

public function index()
{
    $user = 'Auth::user();
    $name=$user->name;
    $role = Role::find(......);   //How to find the  role  of current user ?
    return view('index', compact('name','role'));
}

开斋节-1:

控制器:

public function index()
    {
        $user = 'Auth::user();
        $name=$user->name;
        $role = $user->getRoles();
      //  return view('index', compact('name','role'));
        dd($role);
    }

编辑-1的结果:

Collection {#370 ▼
  #items: []
}

这是空的,为什么呢? @Mahfuz

用户:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract
{
    use Authenticatable, CanResetPassword, HasRoleAndPermission;
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

首先在用户模型中包含 HasRoleAndPermission Trait

然后你可以使用这种方法得到所有

$user->getRoles()

它会返回将所有角色用作集合。