Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skovmand's avatar

Config:cache makes variables in .env null

I'm confused and hope you can help!

After I run php artisan config:cache the variables set in my .env-file are not being read properly in the application and if I do php artisan tinker I get:

>>> env('S3_KEY');
=> null

after doing php artisan config:clear everything is back to normal:

>>> env('S3_KEY');
=> "AERIAOEBNOIAERGOIEAJRI" // not my real key

How come config:cache has this effect? Is it because I use e.g. S3_KEY in a config.php-file? Which is then set to null?

1 like
15 replies
StormShadow's avatar
Level 51

@skovmand

Relevant ?

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.

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.

16 likes
skovmand's avatar

Thanks. That's it. I have been pulling my hair out over this one.

MikeHopley's avatar

It's actually semi buried in the docs:

I was looking for that (for the other thread about config), and I just couldn't find it. I thought maybe I'd imagined it!

The Laravel docs are really well written, but a bit patchy in places.

screenager's avatar

Worth mentioning is that if you did call config:cache during local development, you can undo this by deleting the bootstrap/cache/config.php file

5 likes
MikeHopley's avatar

Worth mentioning is that if you did call config:cache during local development, you can undo this by deleting the bootstrap/cache/config.php file

Or you could artisan config:clear.

3 likes
santacruz's avatar

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.

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.

Can anyone explain this further? What's the reason behind this change? And does it mean I cannot use env('SOME_VAR') at all anymore directly in my code except in the config files? Will it work or will it break if I utilize the config cache?

I find the explanation quite confusing...

3 likes
mcmaxx's avatar

I ended up adding each one into config/app.php (and referencing it as env('variable_name')) and just calling it properly with config('variable_name') from within the code

I think the main reason behind it is allowing users to set a default if the env file is missing the value requested

1 like
larayaume's avatar

This saved me after hours of struggle. Thank you!

Please or to participate in this conversation.