Home > Backend Development > PHP Tutorial > Laravel 5.2 roles包的使用

Laravel 5.2 roles包的使用

WBOY
Release: 2016-06-06 20:16:32
Original
1303 people have browsed it

在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>
Copy after login
Copy after login

控制器:

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

dd()结果:

<code>Collection {#370 ▼
  #items: []
}</code>
Copy after login
Copy after login

回复内容:

在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>
Copy after login
Copy after login

控制器:

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

dd()结果:

<code>Collection {#370 ▼
  #items: []
}</code>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template