Configuring multiple sets of environment variables in Laravel 5

Edit: This question was applied to testing, but essentially I would like to know if it is possible to have more than one .env file that can define several sets of environment variables; how L4 did it with env.local.php , env.testing.php , etc.


I am using Laravel 5 and developing in the estate box.

I have a .env file populated with my local environment variables (mysql database, etc.).

I want to use the sqlite database for testing, so I added some variables in phpunit.xml so that it looks like this:

 <php> <env name="APP_ENV" value="testing"/> <env name="CACHE_DRIVER" value="array"/> <env name="SESSION_DRIVER" value="array"/> <env name="DB_DRIVER" value="sqlite" /> <env name="DB_PATH" value="database.sqlite" /> </php> 

All relevant environment variables are referenced in configuration files, for example. env('DB_DRIVER') .

However, when I try to migrate the database using php artisan migrate --env=testing , I get "do not migrate anything", suggesting that Laravel is trying to migrate my local (mysql) database (which is already migrated).

I decided to explore further through messing around, so I ran php artisan tinker --env=testing . Inside tinker I ran app()->environment(); which returned "testing". So far so good!

Next, I extracted env variables through $_ENV , and it returned the environment variables local , despite the fact that the environment was configured for testing.

Why is this? It seems that Laravel ignores the env phpunit.xml variables and just uses the .env file no matter ...

Edit

It seems that laravel uses the phpunit.xml file if it is run through phpunit, even if the environment is configured for testing separately.

If anyone knows how to install multiple environments in L5, I appreciate this, as the way L4 only make the .env.testing file .env.testing not work.

+5
source share
2 answers

A quick workaround for the original question, only for the test environment case, is to call the class $this->artisan('migrate'); from tests/TestCase.php , so it will get the database configuration values ​​defined in phpunit.xml and do the migration.

+1
source

Define environment variables in the .env file. For instance:

 DB_HOST=localhost DB_DATABASE=database_name DB_USERNAME=homestead DB_PASSWORD=secret 

Then you can access this variable using

 env('DB_HOST') 
-2
source

All Articles