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.
Set the connection in the User.php model or an Auth-related model.
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:
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:
$connection = 'connection name'
protected $connection = 'connectionname';
Set the connection in the User.php model or an Auth-related model.
First, you should define the connection in config/database.php:
Then add them in
auth.php
:Define protected
$connection = 'connection name'
in each verifiable model, and finally use in the model: