Home  >  Q&A  >  body text

How to modify database connection in auth.php?

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粉611456309P粉611456309430 days ago540

reply all(2)I'll reply

  • P粉393030917

    P粉3930309172023-09-09 17:59:24

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

    reply
    0
  • P粉026665919

    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';

    reply
    0
  • Cancelreply