你可以做到这一点,最好的办法是在你的user.php的模式:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token','is_admin',
];
protected function getRole(){
if(\Session::has("role")){
return Session::get("role");
}else{
//calculate and save role in session
}
}//end get Role
}//end class
,所以你会检查它像这样
Auth::user()->getRole();
确保在会话保存登录后的作用,getRole会得到它并附加到用户()对象,这也不需要中间件 希望它有帮助
Auth :: user()在每个请求上重新创建或重新加载,所以我建议你存储你的c在会话中计算角色,然后创建一个中间件,检查角色是否存在,然后将该角色附加到该中间件中的Auth :: user()对象中...希望它有帮助 – razzbee
好吧,我刚刚阅读了有关中间件,这看起来像我在找什么。但是可以告诉我如何将一个额外的属性附加到Auth :: user(),或者你可以指向正确的方向。是否像“$ request-> user() - > role = 23”? –