[英]How to find current user's role in Laravel5.2?
I am using package "romanbican/roles" in Laravel 5.2, 我在Laravel 5.2中使用软件包“ romanbican / roles”,
How to find current user's role ? 如何找到当前用户的角色?
Controller: 控制器:
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'));
}
Controller: 控制器:
public function index()
{
$user = \Auth::user();
$name=$user->name;
$role = $user->getRoles();
// return view('index', compact('name','role'));
dd($role);
}
result of edit-1: edit-1的结果:
Collection {#370 ▼
#items: []
}
It's null,why is it? 它为空,为什么呢? @Mahfuz
@Mahfuz
User: 用户:
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract
{
use Authenticatable, CanResetPassword, HasRoleAndPermission;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
first include HasRoleAndPermission Trait in your user model 首先在用户模型中包含HasRoleAndPermission Trait
then you can get all using this method 那么您可以使用此方法获得全部
$user->getRoles()
it will return user all roles as collection. 它将返回用户所有角色作为集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.