144

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Fusion
  • 5,046
  • 5
  • 42
  • 51

13 Answers13

306

Since Laravel 5.2, the env(...) function will not work after you cached the config.

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

For a quick fix this will do:

 php artisan config:clear

But it will fail again as soon as configuration is cached, as should always be the case in production environments.

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
  • 2
    Thank you, `php artisan config:clear` is the only thing that worked to get my AWS SES credentials to read. – Joe Scotto Mar 07 '20 at 18:05
  • 11
    So I think we should not directly use something like `env('APP_ENV') ` in our application. Instead, we should write a line in `config/app.php`: `'env' => env('APP_ENV'),`. Then, we use it in our code: `config('app.env')`. – Buu Pham May 22 '20 at 02:00
  • 1
    @cosmos for anyone reading this, ALWAYS do that, `env` is only useful in any `config/*.php` file, never outside !!!! – matiaslauriti Apr 30 '21 at 00:06
  • "There are no commands defined in the "config" namespace." Still Laravel does not read the .env file – feeela Jan 25 '22 at 13:51
  • cache clear did it for me – Tim Bogdanov May 26 '23 at 15:07
61

Hope this command will save you

php artisan config:clear

sh6210
  • 4,190
  • 1
  • 37
  • 27
48

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

php artisan optimize:clear
Santosh Kumar
  • 1,756
  • 15
  • 24
20

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.

PhoenixRebirthed
  • 462
  • 6
  • 15
16

Just run this command at your Laravel project root

rm bootstrap/cache/config.php
Igor
  • 755
  • 1
  • 10
  • 22
14

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

php artisan config:cache
vpdeva
  • 303
  • 2
  • 11
  • 23
    Some users may need to do php artisan config:clear instead – PhoenixRebirthed Jul 11 '17 at 15:02
  • 7
    Laravel 5.5 I had to run 'php artisan config:clear'. Thanks – dacastro4 Oct 24 '17 at 16:18
  • 4
    php artisan config:cache is the reason why env() values are null. If you wish to cache your config and env vars then you shouldn't use env() anywhere else except config files because once cached env() will always return null. Although now I am also seeing config() and Config::get() helper return null after caching which is weird too. – BRBdot Jul 26 '18 at 14:40
  • follow up to above - ...So thats when you should realize that config() helper cannot directly access values in .env either. You will need to create or use existing config/.php config file and create your config entry from .env and then use that mapped value. – BRBdot Jul 26 '18 at 16:33
3

After trying below commands

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

If you still get null from env('APP_ENV') then try this app()->environment(). Hope this will don't return null. This works for me

Lokman Hosen
  • 342
  • 5
  • 6
2

Unfortunately, the accepted solution does not provide an appropriate solution for this problem!

The case: Since Laravel 5.2, if you call env("key") directly, it will always return null if you executed php artisan config:cache as last command.

Accepted answer say : execute php artisan config:clear as last command to solve problem! This is not a solution!, because we need always to execute php artisan config:cache as last command..

So what is the solution?

Here i need to explain solution for two of cases:


Case 1 (Predefined keys): If you need to get value from .env file which created by Laravel project as (default .env file), or by third party plugin you already installed it and follow the correct installation procedure of installation:

  1. Get the key name which listed in .env file and search for it in files of laravel_root/config folder (remember: all files in config folder returns results as array), for example i searched for APP_URL which per-defined by Laravel project as default:

enter image description here

As you say here I found tow results in tow config files, one in filesystem.php and other in app.php, so to get any of these values in any php (Classes or view files, ... etc) on your laravel project just call it like this:

  • For result which we found in app.php file, get it value by:

config('app.env');

Here app is name of php file, and env is the key of array which returned as result of app.php file.

  • For result which we found in filesystems.php file, get it value by:

config('filesystems.public.url');

Here filesystems is name of php file, and public is the key (level 1) and url is the key (level 2) of array which returned as result of filesystems.php file.

Summary of this case: if you need get value of APP_URL in any php file of Laravel project you can get it by call config('app.env') function, whether the last command executed is php artisan config:clear or php artisan config:cache, It doesn't matter at all.


Case 2 (Not predefined keys - if you need generate new custom environment key): See this answer for explain: https://stackoverflow.com/a/69707739/2877427

AnasSafi
  • 5,353
  • 1
  • 35
  • 38
1

Remember to check your .env file for duplicate variable declarations.

STRIPE_PUBLISHABLE_KEY="why-wont-this-key-get-outputted"

#...

# Because at the bottom of the .env file it is unset like this
STRIPE_PUBLISHABLE_KEY=
johncarter
  • 71
  • 8
0

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

vimuth
  • 5,064
  • 33
  • 79
  • 116
0

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.

Sachithd
  • 470
  • 6
  • 6
0

php artisan config:clear. Please run this command it will clear the config cache. Its happening because the config file is cached and at the time of caching the values must be null.

vivek rautela
  • 135
  • 2
  • 7
0

Always clear config cache whenever any modification is made on .env or any other config files

php artisan config:clear
muhsin_ap
  • 62
  • 1
  • 1
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '22 at 06:02