Home > Blockchain >  Changing the connection for the user model
Changing the connection for the user model

Time:09-05

Laravel 9, Sanctum Hi, I have specified the eloquent driver in the auth.php provider. eloquent driver in the auth.php In the model itself, I indicated the connection I needed. User Model

But for some reason auth.php still uses the default database :/.

CodePudding user response:

Also you need to define your connection configuration in config\database.php

.env

DB_AUTH_DATABASE=db_auth
DB_AUTH_USERNAME=root
DB_AUTH_PASSWORD=

config\database.php

'connections' => [

    // ...

    'mysql_auth' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_AUTH_DATABASE', 'forge'),
        'username' => env('DB_AUTH_USERNAME', 'forge'),
        'password' => env('DB_AUTH_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

    // ...

]

CodePudding user response:

Add this in your projects .env

DB_CONNECTION_AUTH=database_auth
DB_HOST_AUTH=127.0.0.1
DB_PORT_AUTH=3306
DB_DATABASE_AUTH=database_auth
DB_USERNAME_AUTH=root
DB_PASSWORD_AUTH=

config\database.php add your connection in connections array.

   'connections' => [
             'database_auth' => [
            'driver' => 'mysql',
            'url' => env('DB_URL_CONNECTION_AUTH'),
            'host' => env('DB_HOST_AUTH', '127.0.0.1'),
            'port' => env('DB_PORT_AUTH', '3306'),
            'database' => env('DB_DATABASE_AUTH', 'forge'),
            'username' => env('DB_USERNAME_AUTH', 'forge'),
            'password' => env('DB_PASSWORD_AUTH', ''),
            'unix_socket' => env('DB_SOCKET_AUTH', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
     ]

Now in your Model

protected $connection ="database_auth";

Clear app cache , Restart your app. Done.

  • Related