Laravel, Auth::user() in controller

后端 未结 9 1688
说谎
说谎 2021-02-20 11:44

Laravel framework- Why aren\'t I able to use Auth::user(), (to see if user has been logged in) in the controller of the laravel project. Is the Session not connected to the cont

9条回答
  •  鱼传尺愫
    2021-02-20 12:02

    If you're on Laravel 5.x, you'll probably have the auth middleware component. That might be the missing link.

    I often use a pattern like this in my controllers:

    public function __construct()
        {
            $this->middleware('auth');
            $this->user =  \Auth::user();
        }
    

    then you can

    if ($this->user .. )

    or

    if ($this->user->can('something')) with Entrust or similar

提交回复
热议问题