using auth in laravel service provider

后端 未结 1 1000
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 02:38

whenever i tried to use \\Auth::User() i am getting non object property because my Auth::guest() returns true whenever i use them in service provid

相关标签:
1条回答
  • 2020-12-20 03:21

    You probably want to use a View Composer for this. As far as I know the authenticated user is not yet available in your service providers boot method.

    public function boot(Guard $auth) {
        view()->composer('*', function($view) use ($auth) {
            // get the current user
            $currentUser = $auth->user();
    
            // do stuff with the current user
            // ...
    
            // pass the data to the view
            $view->with('currentUser', $currentUser);
        });
    }
    

    Code modified from https://laracasts.com/discuss/channels/general-discussion/how-do-i-get-the-current-authenticated-user-laravel-5

    0 讨论(0)
提交回复
热议问题