Support the ongoing development of Laravel.io →
Configuration Database Testing
Last updated 2 years ago.
0

Bumping this instead of creating duplicate thread.

0

You use the env() method and the .env file. env() takes two arguments the envrinment variable you want to look up and the default if that variable doesn't exist.

So for example

$session_driver = env('SESSION_DRIVER','array');

In your .env file you would have

SESSION_DRIVER=file

So this means that $session_driver would be = file but if there was no entry in the .env file then $session_driver would be = array

So in your example of multiple DB connections

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('MYSQL_DB_HOST', 'localhost'),
    'database'  => env('MYSQL_DB_DATABASE', 'forge'),
    'username'  => env('MYSQL_DB_USERNAME', 'forge'),
    'password'  => env('MYSQL_DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

'pgsql' => [
    'driver'   => 'pgsql',
    'host'     => env('PGSQL_DB_HOST', 'localhost'),
    'database' => env('PGSQL_DB_DATABASE', 'forge'),
    'username' => env('PGSQL_DB_USERNAME', 'forge'),
    'password' => env('PGSQL_DB_PASSWORD', ''),
    'charset'  => 'utf8',
    'prefix'   => '',
    'schema'   => 'public',
]

In your .env file

MYSQL_DB_HOST=localhost
MYSQL_DB_DATABASE=foo
MYSQL_DB_USERNAME=bar
MYSQL_DB_PASSWORD=baz

PGSQL_DB_HOST=localhost
PGSQL_DB_DATABASE=baz
PGSQL_DB_USERNAME=bar
PGSQL_DB_PASSWORD=foo

There is a Laracast on the subject as well. https://laracasts.com/series/laravel-5-fundamentals/episodes/6

Last updated 9 years ago.
0

That Laracasts link is pure gold. Much appreciated.

0

Thank you for your answer, it helped me a lot.

Although, I still find the old way cleaner.

0

Thanks, mikerh. That was very helpful!

How about Part A of thytanium's question? i.e. I want to use the mysql driver for prod and sqlite for testing (if I run phpunit, hit the sqlite db not otherwise). How does dotenv accomodate this? Do we need to add a conditional to the default key to make this happen?

Thanks!

0

Finally figured out a clean solution to the problem of wanting to use different db's for dev and testing in Laravel 5. In case anyone else is interested, here goes:

  • in config/database.php, replace the default db line with
'default' => env('DATABASE_DRIVER','mysql'),
  • In the .env file, add the following at the end
DATABASE_DRIVER=mysql
  • in phpunit.xml, add the following line inside the <php> section
<env name="DATABASE_DRIVER" value="sqlite"/>

Essentially, what you are doing is setting prod and testing defaults for the key named "DATABASE_DRIVER" and then using whatever is set in database.php.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

thytanium thytanium Joined 7 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.