php - Laravel env() function is not reading from .env file - Stack Overflow

I have this strange condition where "env()" function is not reading from .env file.I'm

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
  • Can you check if bootstrap/cache/config.php exists? Usually, the php artisan optimize:clear command should delete the cached configs, but since you mention env() is not working, the cache might still exists. – Tatachiblob Commented Mar 27 at 7:24
  • is this specific to some env key or for all? – kris gjika Commented Mar 27 at 9:37
  • 1 Where are you calling env()? If it's anywhere outside of a config/*.php file, you shouldn't do that, since if you're running php artisan cache:config, env() will only work in config/*.php files. – Tim Lewis Commented Mar 27 at 17:51
  • From laravel/docs/12.x/configuration#configuration-caching, "If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env() function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded; therefore, the env() function will only return external, system level environment variables." (Emphasis mine) – Tim Lewis Commented Mar 27 at 17:54
  • 1 "But modifying the existing variable from the .env file doens't work." - what does that mean? Why can't you edit the file? – Nico Haase Commented Apr 3 at 7:03
 |  Show 7 more comments

2 Answers 2

Reset to default 0

1、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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信