简体   繁体   中英

Laravel 8 : Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given

I am trying to create multiple guard authentication for Users and Admins for my application. Before you mark it as duplicate or something, please read it and let someone help me. I have tried all the possible solutions from same issues solutions but nothing worked till now.

Here is my code:

auth.php

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

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

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

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

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Models\AdminUser::class,
    ],
],

AdminUser.php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;


class AdminUser extends Authenticatable
{
    use HasFactory, Notifiable;
    protected $table = 'admin_users';

    protected $guard = 'admin';

    protected $fillable = [
        'first_name',
        'middle_name',
        'last_name',
        'email',
        'password',
    ];


    protected $hidden = [
        'password',
        'remember_token',
    ];


    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

AdminAuthController.php

class AdminAuthController extends Controller {
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function getLogin()
    {
        return view('login');
    } 
}

web.php

Route::get('/login', 'LoginController@index')->name('login');
Route::get('/admin/login','AdminAuthController@getLogin');

When I go to my-application/login it ask for username and password and let me in once authenticated. But when I go to my-application/admin/login , it gives me this error:

Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in /var/www/vhosts/my-application/httpdocs/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 124

I tried solutions with the same issue, but nothing helped.Laravel documentation is bit confusing and so I am here to seek your help. Please help me out and I appreciate your help in advance. I am using Laravel 8.*

I faced this issue just yesterday. I am relatively new to web development. It turned out the issue arose from my cofig/auth.php. In setting the "guards" section, I set the provider to reference the table name ('provider' => 'admins_user') rather than the "providers" section below it ('provider' => 'admins'). Solution: When I pointed guards to the "providers" section, it was resolved. I know it's a silly mistake on my side but I hope this helps someone out there with similar circumstances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM