"what's the correct way to set env variables in laravel 5?" Answer’s


You can do it exactly the same as in Laravel 4:

$env = $app->detectEnvironment(array(
    'local' => array('homestead')

*.env file are just used to put sensitive data that shouldn't be put into VCS. The same is in Laravel 4

but is seems that in last days default detectEnvironment was changed to:

$env = $app->detectEnvironment(function()
    return getenv('APP_ENV') ?: 'production';

so you can use either setting variable from PC name or from ENV file.

If you use ENV based environment detection in main env file (by default .env file you need to add:


Of course local here is local environment, you can change it into production or dev

At the moment the most important issue I see is that you need to remember when going on production to change this .env file content from APP_ENV=local to APP_ENV=production so in my opinion much better method is the old default method based on PC names.

Now ENV files. If you use ENV based environment detection, you should put into your ENV file only:


Now you can create separate ENV files for your different environments for example:

.local.env :


.production.env :


and now in bootstrap.environment.php file you can modfiy:

if (file_exists(__DIR__.'/../.env'))


if (file_exists(__DIR__.'/../.env'))

    if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
        Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');

to load extra env file based on APP_ENV from main env file.

Now you will be able to use it in your other configuration file as always: $_ENV['MY_DB']

Wednesday, March 31, 2021
answered 11 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :