当前位置:Gxlcms > PHP教程 > Laravel5.2roles包的使用

Laravel5.2roles包的使用

时间:2021-07-01 10:21:17 帮助过:33人阅读

在laravel5.2中用roles这个包,想获得当前登录用户的role,得到一个空集合,为啥?
模型:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract

{
    use Authenticatable, CanResetPassword, HasRoleAndPermission;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

}

控制器:

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

dd()结果:

Collection {#370 ▼
  #items: []
}

回复内容:

在laravel5.2中用roles这个包,想获得当前登录用户的role,得到一个空集合,为啥?
模型:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract

{
    use Authenticatable, CanResetPassword, HasRoleAndPermission;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

}

控制器:

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

dd()结果:

Collection {#370 ▼
  #items: []
}

如果没有addRoles默认就是空的啊……

人气教程排行