How to modify database connection in auth.php?
P粉611456309
P粉611456309 2023-09-08 23:30:46
0
2
539

How to change the database in Laravel's auth.php (config/auth.php)? I'm working with multiple databases and want to store users in another database.

P粉611456309
P粉611456309

reply all(2)
P粉393030917

Set the connection in the User.php model or an Auth-related model.

P粉026665919

First, you should define the connection in config/database.php:

'connections' => [
    'global' => [
            'driver' => 'mysql',
            'host' => env('first_db_name', '127.0.0.1'),
            ...
            ],
    'tennant' => [
            'driver' => 'sqlite',
            'host' => env('sec_db_name', '127.0.0.1'),
            ],
    ]

Then add them in auth.php:

'guards' => [
    [...]
    'global' => [
        'driver' => 'session',
        'provider' => 'globals',
    ],
    'tennant' => [
        'driver' => 'session',
        'provider' => 'tennants',
    ],
],

[...]
'providers' => [
    [...]
    'globals' => [
        'driver' => 'eloquent',
        'model' => App\UserGlobal::class,
    ],
    'tennants' => [
        'driver' => 'eloquent',
        'model' => App\UserTennant::class,
    ],
],

Define protected $connection = 'connection name' in each verifiable model, and finally use in the model:

protected $connection = 'connectionname';
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!