Home  >  Article  >  Backend Development  >  Laravel 5.2 roles包的使用

Laravel 5.2 roles包的使用

WBOY
WBOYOriginal
2016-06-06 20:16:321272browse

在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默认就是空的啊……

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn