Rishad
Rishad

Reputation: 381

How to check current authentication guard and its guest in laravel blade template?

I am working on a Laravel Project. i have 2 authentication guards. This is the first time i am working with multiple authentication guard.

Usually on front end i use to separate code for Guest and Logged in Users as:

@guest
    //Data for Guest Users.   
@endguest

@auth
    //Data for Logged in Users.
@endauth

Now with multiple Guards, @guest is only false when i logged in to system with default guard. ie, @guest is active even when i am logged in through 'Admin Guard'. How can i check if a user is a guest for Admin Guard or if user logged in through Admin guard?

Upvotes: 3

Views: 2406

Answers (1)

compulsive coder
compulsive coder

Reputation: 164

It depends on what your guards names are. Usually 'web' is the default guard in laravel. and it will be mentioned at auth.php inside the config directory as:

defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

So when ever you check @guest or @auth, Guard of 'web' is checked. To Check for a different guard, say 'admin', you can specify it as:

@guest('admin')
    //Data for Guest Users of admin guard.   
@endguest

@auth('admin')
    //Data for Logged in Users of admin guard.
@endauth

Hope this Helped your doubt. feel free to ask more if you have any doubts on this. Also make sure to check community before posting questions. As similar questions like this have already been answered.

Upvotes: 2

Related Questions