Home > OS >  How can i change the database in auth.php?
How can i change the database in auth.php?

Time:09-05

how can i change the database in laravel auth.php (config/auth.php)? I am using multiple databases and want the users to be stored in another.

CodePudding user response:

first you should define connections 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 authenticable model and finally using in model:

protected $connection = 'connectionname';

CodePudding user response:

Set the Connection in model User.php or the model related to Auth.

  • Related