Laravel multi auth - Authentication user provider

2019-07-10 10:35发布

问题:

It works when it put

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

But It shows error when I set a different table

'merchant' => [
        'driver' => 'session',
        'provider' => 'merchants',
    ],

Error: InvalidArgumentException in CreatesUserProviders.php line 40: Authentication user provider [] is not defined.

What is the problem. Would you please explain me ? Thanks.

回答1:

At first did you create your merchants providers?

if you didnt

php artisan make:provider MerchantServiceProvider

Then you have to register your provider in: config/app.php/

'providers' => [ App\Providers\RouteServiceProvider::class ]


回答2:

If you want to change the register / authentication table go back with your changes, you only need to change in the config/auth.php file

    'providers' => [
            'users' => [
                'driver' => 'eloquent',
/*change this ---->*/ 'model' => App\<modelclassname>::class,
            ],

This are the Authentication user provider, so if what you want is to add a new provider, you need to add another provider to this array. The error that you have is because the provider mechants that you are calling is not registered here.

And check that your /app/modelname.php has the protected table created like this:

protected $table = 'merchant';  // ----> this is your table name

So if you want to use multiple providers, you should register your provider in the user provider section of the config/auth.php file. Your provider array should look like this:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Users::class,
        ],
        'merchant' => [
            'driver' => 'eloquent',
            'model' => App\Merchant::class,
        ],
    ],

After that you should able to use the provider as you write in the question.



回答3:

You can always use my package https://github.com/Hesto/multi-auth and focus on your application instead of this annoying configuration.