在laravel5.2中用roles这个包,想获得当前登录用户的role,得到一个空集合,为啥?
模型:
<code>class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract { use Authenticatable, CanResetPassword, HasRoleAndPermission; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; }</code>
控制器:
<code>public function index() { $user = \Auth::user(); $name=$user->name; $role = $user->getRoles(); // return view('index', compact('name','role')); dd($role); }</code>
dd()结果:
<code>Collection {#370 ▼ #items: [] }</code>
在laravel5.2中用roles这个包,想获得当前登录用户的role,得到一个空集合,为啥?
模型:
<code>class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract { use Authenticatable, CanResetPassword, HasRoleAndPermission; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; }</code>
控制器:
<code>public function index() { $user = \Auth::user(); $name=$user->name; $role = $user->getRoles(); // return view('index', compact('name','role')); dd($role); }</code>
dd()结果:
<code>Collection {#370 ▼ #items: [] }</code>
如果没有addRoles默认就是空的啊……