Laravel 无法在构造函数中设置 class 变量

Laravel can't set class variable in constructor

我的 class 中有一个私有变量,我想将其设置为经过身份验证的用户。但是,出于某种原因,我无法在构造函数中设置变量。这是我目前所拥有的:

private $user = null;


  public function __construct()
    {
        $this->middleware('auth');

        $this->user = Auth::user();
    }

 public function index()
    {
        $return['Admin'] = $this->user;

        return view('home', compact('return'));
    }

变量 $user 出于某种原因保持为空。但是,当我使用 Auth::user() 代替 $this->user 时,它工作得很好。过去我多次从构造函数设置变量,这是第一次它对我不起作用。任何帮助将不胜感激。

由于 Laravel 5.3 您无法再访问控制器构造函数中的会话,because middleware has not run yet

You can define a closure(滚动到 "Session In The Constructor")在会话中间件具有 运行.

之后发生