Laravel does not read env variables

please use this its work for me use git bash or cmd and past this command

$ rm bootstrap/cache/config.php

this command clear cache folder


The solution is simple, but neither the IDE nor the debugger says anything about it. It just returns null. When you use php artisan config:cache, according to the documentation:

If you execute php artisan config:cachecommand during your deployment process, you should be sure that you are only calling the env() function from within your configuration files.

Obviously I have env variables outside the config files, so after caching I was not able to use it outside anymore. The php artisan config:clear puts it back to work.

What I've found more about the usage of env, that it should be used only within config files. You can access env variables from the rest of the project using other helper method config(). Be sure to assign it to another key in config file, e.g. 'key' => env('CACHE_DRIVER')

What is more, you have to remember to run php artisan config:cache each time you will change .env file. Laravel will not load the new values, until it's cached. If it's not cached, no need to do it.


Run those of command

composer dump-autoload
php artisan cache:clear
php artisan config:clear
php artisan view:clear

Now try to read

$value = env('VARIABLE_NAME');

if not working till now then,

Try another syntax to read env variable.

$value=getenv('VARIABLE_NAME');

Just run

php artisan config:clear

It solved my issue.