auth()-> user()在ServiceProvider中返回null

时间:2020-04-08 07:25:54

标签: laravel

我尝试在ServiceProvider中获取用户ID,但是auth()-> user()成功登录后返回null,这是哪里出了问题?

它不仅在测试模式下起作用

public function boot()
    {
        dd(auth()->user()); // => return null
        try {
            Permission::get()->map(function ($permission) {
//                dd('$permission');
                Gate::define($permission->slug, function ($user) use ($permission) {
                    return $user->hasPermissionTo($permission);
                });
            });
        } catch (\Exception $e) {
            report($e);

}

1 个答案:

答案 0 :(得分:0)

尝试使用:

use Auth;
public function boot(){
   Auth::user(); //or
   Auth::user()->name; //or
   Auth::user()->get();
}