2
我在Laravel 5.2中使用软件包“romanbican/roles”,
如何找到当前用户的角色?如何在Laravel5.2中查找当前用户的角色?
控制器:
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'));
}
eidt-1:
控制器:编辑-1的
public function index()
{
$user = \Auth::user();
$name=$user->name;
$role = $user->getRoles();
// return view('index', compact('name','role'));
dd($role);
}
结果:
Collection {#370 ▼
#items: []
}
这n值为什么呢? @Mahfuz
用户:
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract
{
use Authenticatable, CanResetPassword, HasRoleAndPermission;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
https://laracasts.com/discuss/channels/general-discussion/check-if-user-has-admin-role – C2486
@LearningMode我通过dd()获得了一个没有任何东西的集合。 – sunshine
@ sunshine在'users'表中有'role'列吗?如果没有,请创建一个。如果存在改变'$角色= $ user-> getRoles();'到'$角色= $ user->角色;'和'dd($角色);' – smartrahat