-2

Do I need to have my DB connection info in 2 places? If I do the below, I connect just fine. If I remove either file's data, I can't connect.

config/databases.php file

    'blah_1' => [
        'driver' => 'mysql',
        'host'      => env('DB_HOST’,’1.1.1.1’),
        'port'      => env('DB_PORT','3306'),
        'database'  => env('DB_DATABASE’,’someDB_1’),
        'username'  => env('DB_USERNAME’,’someUser_1’),
        'password'  => env('DB_PASSWORD’,’somePass_1’),
    ],

    'blah_2' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST_SECOND’,’2.2.2.2’),
        'port'      => env('DB_PORT_SECOND','3306'),
        'database'  => env('DB_DATABASE_SECOND’,’someDB_2’),
        'username'  => env('DB_USERNAME_SECOND’,’someUser_2’),
        'password'  => env('DB_PASSWORD_SECOND’,’somePass_2’),
    ],

.env file:

DB_CONNECTION=blah_1
DB_HOST=1.1.1.1
DB_PORT=3306
DB_DATABASE=someDB_1
DB_USERNAME=someUser_1
DB_PASSWORD=somePass_1

DB_CONNECTION_SECOND=blah_2
DB_HOST_SECOND=2.2.2.2
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=someDB_2
DB_USERNAME_SECOND=someUser_2
DB_PASSWORD_SECOND=somePass_2
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
bart2puck
  • 2,432
  • 3
  • 27
  • 53

1 Answers1

3

The short answer is no. You only need it in your config/databases.php. The .env file is to overwrite the settings in your other environments without updating the configuration file.

For example, in your local environment, your credentials is most likely different from your production environment. You wouldn't want to update config/databases.php locally and remind yourself to not push the file.

However, the connections would still work even if remove them from the .env file. It will use the second parameter's value in your env() as default.

Chin Leung
  • 14,621
  • 3
  • 34
  • 58