D
D
Dmitry Evgrafovich2015-04-02 10:16:37
Laravel
Dmitry Evgrafovich, 2015-04-02 10:16:37

Service providers in Laravel - how to know if a user is logged in?

I decided to connect the menu through the service provider, here is the code that I insert into the boot method:

if (\Auth::guest()) {
            $links = '1';
        } else {
            $links = '2';
        }
        \Debugbar::info(\Auth::guest());
    view()->composer('menu.menu', function($view) use ($links) {
            $view->with('links', $links);
        });

The problem is that \Auth::guest() returns true whether the user is logged in or not. Tell me what I missed?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2015-04-02
@Tantacula

Replace with \Auth::user()
Then just like this:

dd($auth->user()); // null

        view()->composer('partials.nav', function($view) use ($auth){
            dd($auth->user()); // returns User object
            $view->with('currentUser', $auth->user()); // does what you expect
        });

P
Peter, 2015-04-02
@petrtr

Auth::user() needs to be checked

S
shane89, 2015-10-15
@shane89

guest of course will be true
normal check

@if(Auth::check())
//действие
@endif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question