0
votes

I have created a custom guard called 'business_user' in my Laravel app by adding the provider into my auth.php file.

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

     'business_users' => [
         'driver' => 'eloquent',
         'model' => App\BusinessUser::class,
     ],
],

In my blade template I have some custom code which display different content depending on the user that has logged in.

@if (Auth::check('business_user'))
    <a class="btn btn-light" href="{{ route('user.logout') }}">Logout Business</a>&nbsp;
@elseif(Auth::check())
    <a class="btn btn-light" href="{{ route('user.logout') }}">Logout Customer</a>&nbsp;
@else
    (Guest user)
@endif

Whether I am logged in as a normal user OR the a business user the blade template always shows the first condition (Logout Business).

I have verified the correct user guard is logged in because I can verify this checking the session variable names in my laravel debug bar.

Can anyone explain why?

1

1 Answers

0
votes

The check method does not take arguments. If you want a specific guard that is what the guard method is for:

Auth::guard('whateveryouwant')->check();

In short your if condition and elseif were checking the same value. When you call check directly on the Auth facade the Auth Manager will use the default guard as the guard and call that method on it. (The default guard can change at run time; the auth middleware will set the default guard)

Also, you are showing the user providers not the guards that are defined. A guard would be a combination of a provider and a driver.