worgor's avatar

ENV not returning values from .env file on occasion

I'm using Laravel 7.x and 8.x and constantly running into a problem where the ENV command ist taking the default value and not the value in the .env file - the strange thing is it happens from time to time and not constantly. My question is how can i prevent this behavior since this is really an error trap and i've tried to

php artisan cache:clear php artisan config:clear

composer dump-autoload

really eveything which i found but it turns out it is not realiable - e.v. i'm importing data into Elastic Search and the index changes from Application to Application but from time to time the index is the default index which is not really always available.

Thanks Andy

0 likes
14 replies
vincent15000's avatar

Where are you trying to get the env values ?

You can use the env() helper only in a configuration file and nowhere else.

For example it doesn't work in a model or a controller.

1 like
cwhite's avatar

@vincent15000, I may be wrong, but I believe this is technically incorrect.

You can use env() in other places outside the config files, but it's best practice not to (and to use config() instead) because the config values are cached and lookup times are faster.

2 likes
cwhite's avatar

@vincent15000, it works if you don't cache your config:

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 and all calls to the env function will return null.

1 like
vincent15000's avatar

@cwhite That's true ... and what are you doing in production ? For better performance, you cache the configuration.

worgor's avatar

Oh you mean in a controller env() is not working? Then i'm using it wrong - in blade and controllers i guess config('INDEX_XY'); is appropriate? Actually it works in some cases pretty well but i don' t want to misuse it!

1 like
vincent15000's avatar

@worgor Yes you need to retrieve the environment values in a configuration file and then you can retrieve these values from the configuration file.

worgor's avatar

I changed all env(...) calls in the controller to Config::get("elastic.key") and Config::get("app.key") but every now an then Config::get("elastic.key") does not return a value again ... i thought this i cached and once it set there is no problem with consecutive calls? Or am i wrong?

1 like
worgor's avatar

@vincent15000 yes i now changed it - i put the values into a configfile e.g. app or elastic and retrieve it via get

worgor's avatar

@Snapey very good question, how can i recognize if the values are cached? i would say yes but ... the problem is that without a default value the host ist every e.g. 10-15 queries with config::get("host") empty which leads to an import error - i really thought this wasn't the case earlier since i'm using env and config for years now ...

1 like
worgor's avatar

ok this is something i definitly missed - i cached the values now, let's see how it works! Thank your very much for your support to everybody!!!

1 like

Please or to participate in this conversation.