在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預設就是空的啊……