I have this strange condition where "env()" function is not reading from .env file.
I'm on laravel 11 and I have tried the following commands.
php artisan cache:clear
php artisan config:clear
php artisan optimize:clear
I even deleted the whole project folder and restored the files using git.
Still, env() function is not reading from .env file.
I have to reclone the project from git to solve the problem. I have been doing that more than 4 times.
I have found out that adding a new variable to the .env file works. But modifying the existing variable from the .env file doens't work.
What is happening?
I have this strange condition where "env()" function is not reading from .env file.
I'm on laravel 11 and I have tried the following commands.
php artisan cache:clear
php artisan config:clear
php artisan optimize:clear
I even deleted the whole project folder and restored the files using git.
Still, env() function is not reading from .env file.
I have to reclone the project from git to solve the problem. I have been doing that more than 4 times.
I have found out that adding a new variable to the .env file works. But modifying the existing variable from the .env file doens't work.
What is happening?
Share Improve this question edited Apr 3 at 3:10 KWARN KHAM asked Mar 27 at 6:38 KWARN KHAMKWARN KHAM 751 silver badge10 bronze badges 12 | Show 7 more comments2 Answers
Reset to default 01、env()method can get value from :the command
putenv('TEST_ENV=333');
$test = env('TEST_ENV');
2、from the system environment variables,just like .bash_file(macOS)
export ANDROID_HOME="/Users/hello/doc/tool/android/SDK"
echo env('ANDROID_HOME')
will print /Users/hello/doc/tool/android/SDK
3、.env file(If cached will first use cache file)
.env
APP_DEBUG=true
APP_ENV=local
APP_LOCAL=local
Based on the Laravel documentation, you should use env() function only in your config files. if you want to access the environment variables, after you set them in the config files you can access them using config() function, for example:
config("app.your_variable");
The configuration values may be accessed using "dot" syntax, which includes the name of the file and option you wish to access
Please read the docs about config files:
https://laravel/docs/11.x/configuration#accessing-configuration-values https://laravel/docs/11.x/configuration#configuration-caching
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744108187a4558825.html
bootstrap/cache/config.php
exists? Usually, thephp artisan optimize:clear
command should delete the cached configs, but since you mentionenv()
is not working, the cache might still exists. – Tatachiblob Commented Mar 27 at 7:24env()
? If it's anywhere outside of aconfig/*.php
file, you shouldn't do that, since if you're runningphp artisan cache:config
,env()
will only work inconfig/*.php
files. – Tim Lewis Commented Mar 27 at 17:51config:cache
command during your deployment process, you should be sure that you are only calling theenv()
function from within your configuration files. Once the configuration has been cached, the.env
file will not be loaded; therefore, theenv()
function will only return external, system level environment variables." (Emphasis mine) – Tim Lewis Commented Mar 27 at 17:54