In Laravel, always use the env() within config files and nowhere else.

RMAG news

Over the years working with Laravel, I came up with one important rule. If you must utilize the values stored in your .env file, make sure to exclusively employ the env() function solely within your config/*.php files.

“But why?” you might say…

When your configuration is cached in a production environment, such as by executing the php artisan config:cache command, your application compiles the configuration along with any variables from .env into a cache file. This cache file is then exclusively loaded during requests. Consequently, any env() calls within the application will result in null since the variable no longer exists.

Leave a Reply

Your email address will not be published. Required fields are marked *