Laravel 5 – env() always returns null

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.

Leave a Comment