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粉3930309172023-09-09 17:59:24
Set the connection in the User.php model or an Auth-related model.
P粉0266659192023-09-09 15:52:38
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';