dixitchopra's avatar

Setting Environment Variable

How can I set environment value in Laravel 5? I tried setting APP_ENV=local in .env file. In terminal, it is showing local when I type php artisan env.

It shows production when I write this in php file

App::environment()

$app->environment()
0 likes
12 replies
BENderIsGr8te's avatar

What happens in your code if you type this...

echo env('APP_ENV');

From what I can tell in the framework, the .env file is loaded and parsed looking for the environment and it reads directly from that. Perhaps try clearing cache first?

php artisan cache:clear

Than then try re-running your original code?

BENderIsGr8te's avatar

Make sure that APP_ENV only exists ONCE in your .env file. The php dotENV package makes definitions immutable, so once a key has been used, it can not be changed. If APP_ENV is set as "production" at the top of the file, then later in the file APP_ENV is set to "local", then the second one will be ignored.

Also, make sure you don't have any code anywhere that is attempting to change the Environment based on some other variable or check. It too will fail. (This was more common in L4).

dixitchopra's avatar

@BENderIsGr8te I searched for APP_ENV file and got this in compiled.php

class DetectEnvironment
{
        public function bootstrap(Application $app)
    {
        try {
                Dotenv::load($app->basePath(), $app->environmentFile());
        } catch (InvalidArgumentException $e) {
        }
        $app->detectEnvironment(function () {
            return env('APP_ENV', 'production');
        });
    }
}
bestmomo's avatar

There is something odd in your Laravel, could you restart with a fresh install ?

BENderIsGr8te's avatar

As @bestmomo says, something weird is going on. The "return" on the previous post says "If you can't find APP_ENV in the .env file, then set the environment to "production". Is your .env file actually named .env? If you are trying to to call it .env.local.php or anything other than exactly .env (not event .env.php will work) then it won't be read.

You could test this by doing this...

In your .env. file

TEST_SETTING=hello

Then try this in a PHP file from a web browser

echo env('TEST_SETTING', 'Oops, I could not find that setting');

If you see 'hello', then it's at least seeing your .env file. If you see the other message, then .env is not even loading.

anon12822's avatar

I had this problem the other day... I got around this by fresh install of Laravel and copied my files over and that did the trick!

BENderIsGr8te's avatar

That's crummy you had to do all of that. I feel like it would be better to track down the problem because if I ever have this issue on a huge app I don't want to have to resort to doing a fresh install and trying to move all my files over :)

BENderIsGr8te's avatar

The other suggestion was going to be rename the .env file to something else like 'hello.php' and then try to load your app, then rename it back (in the terminal) to .env

Please or to participate in this conversation.