Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - 'Auth guard [admin] is not defined.'

Tags:

laravel

  • This is a fresh copy of Laravel 5.6.
  • using spatie laravel permissions.

I have maybe altered something while dealing with permissions that could have altered the guard. I have no idea what since I can't see any git changes, maybe via the DB?

At first I tried to tinker and give a specific user admin privileges with: $role = Spatie\Permission\Models\Role::where('name', 'admin')->get()->first();

When that returned undefined, I checked Auth::guard('admin'), that returns undefined, so I understand that the issue has nothing to do with Spatie package.

After looking in a few questions/answers here on stackoverflow, one of them being config:clear and config:cache, which didn't work and still getting undefined for Auth guard admin.

Any other options I could take to get Auth admin?

thanks

like image 878
erezt Avatar asked May 13 '18 06:05

erezt


2 Answers

In case you edited your config/auth.php, e.g. to add another guard and your config is cached, your guards may not be reloaded. If you experience this problem, clearing the config will fix it.

https://github.com/laravel/laravel/blob/v5.2.0/config/auth.php

php artisan config:clear OR php artisan config:cache

i'm using laravel 5.4

like image 62
Mayur Panchal Avatar answered Nov 11 '22 07:11

Mayur Panchal


Add admin or administrator to the auth guards. After this php artisan config:clear can be run to remove the previous cached configurations. It should match your roles->name in the database.

\config\auth.php (file)

'guards' => [
    ...

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

An old question, but stil valid from Laravel 5 to 7

like image 31
Heroselohim Avatar answered Nov 11 '22 06:11

Heroselohim