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.
@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.
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.
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!
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?
@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 ...