Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 663
Rishad Avatar asked Jan 20 '26 15:01

Rishad


1 Answers

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.

like image 173
compulsive coder Avatar answered Jan 22 '26 07:01

compulsive coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!