Get custom guard logged name in blade

3.5k Views Asked by At

i'm using custom guard

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'user' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],

i want to get admin name on blade so i call it using

{{ Auth::guard('admin')->name }}

but i got error

Cannot access protected property Illuminate\Auth\SessionGuard::$name

how can i achieve this on laravel 5.3

1

There are 1 best solutions below

0
Tomas Buteler On BEST ANSWER

Are you trying to get the name of the SessionGuard object or the name of the user that is signed in using the admin guard?

This might be what you are looking for:

Auth::guard('admin')->user()->name;

Just note that the above will only work if the user is logged in, as the user method will return null when they're not logged in. Here's how you'd check if they are logged in:

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