2 database connection SQL Server on laravel

Viewed 38

I have 2 databases with SQL Server connections, my .env file is:

DB_CONNECTION=sqlsrv
DB_HOST=1.1.1.1
DB_PORT=1433
DB_DATABASE=Name1
DB_USERNAME=sa
DB_PASSWORD=pass1

DB_CONNECTION_SECOND=sqlsrv
DB_HOST_SECOND=1.1.1.1
DB_PORT_SECOND=1433
DB_DATABASE_SECOND=Name2
DB_USERNAME_SECOND=sa
DB_PASSWORD_SECOND=pass2

And my database.php is:

'sqlsrv' => [
    'driver' => 'sqlsrv',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '1433'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
],
'sqlsvr1' => [
    'driver' => env('DB_CONNECTION_SECOND'),
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST_SECOND', 'localhost'),
    'port' => env('DB_PORT_SECOND', '1433'),
    'database' => env('DB_DATABASE_SECOND', 'forge'),
    'username' => env('DB_USERNAME_SECOND', 'forge'),
    'password' => env('DB_PASSWORD_SECOND', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
],

My problem is I can connect to the first DB but I can't connect to the second DB, I already use php artisan config:cache, php artisan route:clear and also get the name of DB with function boot in AppProvider there's something that I'm missing here?

I'm making a login with 2 DB connections for now I just try to make sure there is a connection I'm using :

in the AppService Provider:

public function boot()
{
    try {

        DB::connection()->getPDO();
        dump('Database name is:'.DB::connection()>getDatabaseName());

    } catch (Exception $e) {

        dump('Database connection error');

    }
}

Only give me the name of the first connection BTW I'm not using migrations because I only have to make requests on the DB.

0 Answers
Related