106
votes

I try to find out why my env() helper always returns null. This causes trouble especially in app.php file, where are env() helpers widely used by default. Perhaps any mysterious server setting?

My env file:

APP_ENV=production
APP_KEY=base64:mymagickey=
APP_DEBUG=false
APP_LOG_LEVEL=info
APP_URL=http://www.example.com

etc...

EDIT - I tried following:

php artisan cache:clear
php artisan view:clear
php artisan config:cache

and ofcourse, i am using env helper like this: env('APP_ENV')

But still no success. The wierd part is, that $_ENV php variable contains every single variable from .env file.

10

10 Answers

227
votes

env(...) function will not work after you cached the config. (starting from laravel 5.2 till current 5.7)

The Laravel documentation says

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

So the correct answer would be to

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

And I quoted it from the same documentation

But for a quick fix this will do:

php artisan config:clear

And now it should be clear why, when you tried config:cache, it did not help, even though it clears the config prior to caching.

52
votes

Hope this command will save you

php artisan config:clear

34
votes

Five most important commands if your Laravel is not working as expected after some modifications in .env or database folder or because of any other modifications. Here is full explanation: https://www.youtube.com/watch?v=Q1ynDMC8UGg

php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload
22
votes

The following Command worked for me. I accidently cleared all the cache files, So env('something') was returning null.

php artisan optimize:clear
16
votes

Use \Config::get('app.env'); instead of env(APP_ENV); because you're going to get the same error eventually and that's not good for a live website.

If you want to add custom variables from your ENV, go into your config app and find this:

/*
    |--------------------------------------------------------------------------
    | Application Environment
    |--------------------------------------------------------------------------
    |
    | This value determines the "environment" your application is currently
    | running in. This may determine how you prefer to configure various
    | services your application utilizes. Set this in your ".env" file.
    |
    */

'env' => env('APP_ENV', 'production'),

add a new line under "'env' => env('APP_ENV', 'production'),", so for example, it could be the following:

/*
    |--------------------------------------------------------------------------
    | Application Environment
    |--------------------------------------------------------------------------
    |
    | This value determines the "environment" your application is currently
    | running in. This may determine how you prefer to configure various
    | services your application utilizes. Set this in your ".env" file.
    |
    */

'env' => env('APP_ENV', 'production'),
'key' => env('APP_KEY'),

You can call the "key" variable like this:

\Config::get('app.key');

Whenever you add a new variable like "key" to the app env, you'll need to use config:cache to reset the cache.

10
votes

It is a ".env" known bug which can be solved with:

php artisan config:cache
3
votes

Just run this command at your Laravel project root

rm bootstrap/cache/config.php
1
votes

If the accepted answer doesn't fix the issue check if .env file has correct read and write permission given. If the file cannot be read by the framework / library then the values will always be null. I've come across this issue on one of my Lumen projects but after changing the file permissions it worked.

0
votes

I know this is a very old thread and the reason may not be the same. But the same issue happened to me.

The reason was I had an issue inside .env file. This is what I got in .env

TEST="e@1asa

Notice that ending quote is missing. And it should be like,

TEST="e@1asa"

All the env variables I added after that returned null due to this error.

Nothing written to error log even. So if you have met this error try adding an example env variable as the first record of the file. If it works and record at last not working there may be an error inside .env

0
votes

Place .env file at the root folder of the project

Add these to app.php file.

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    echo $e;
}