I have been working on an application for a while now, and somehow one morning I noticed the application was no longer reading the Google recaptcha key from the .env file thus preventing me from gaining access to the system.
I thought I had tampered with my code somewhere, I looked back at the recent changes I made and none of them were related to the login aspect of the app.
Here is how I fixed it (I'm putting this out to help other developers as well as an easy access to my solution should I ever run into this kind of error again).
I initially ran the following commands but none of them fixed the issue.
php artisan optimize:clear
php artisan route:clear
To fix the issue, I simply had to clear the configuration cache by running the following command:
php artisan config:clear
and that was it, my application was reading the environment variables from the .env file.
If you found this article helpful, don't forget to leave a like.
Thanks for reading.
Top comments (5)
First you need to clear your cache with this command:
Then you need to create a file in config directory called something like "google.php" and inside you need to return an array like this:
and inside your .env file have declared:
With these definitions you can have cache values for your environment and in your code the right call for the values is this:
And then you can optimize your deployment.
Thanks for this, I'll be sure to take note of this for subsequent projects
You should actually not call environment variables directly since Laravel will not read the .env file after caching. Clearing your cache does not solve the actual problem.
This is documented in the deployment documentation. laravel.com/docs/9.x/deployment#op...
Instead of calling your .env file directly, add a config key to your services.php file so you can benefit from caching.
Thanks so much for this, I have actually implemented this in the application.
I’m experiencing a bizarre and mysterious issue in Laravel. After making changes to the .env file, the changes are not being reflected. I’ve cleared the cache, rebuilt the cache, and even checked using Tinker, but strangely, it still remembers old values that no longer exist in any file.
Even after deleting the config.php file and regenerating it, Laravel still pulls values that don’t exist in the .env file anymore. I used a program that scans through the contents of all files to check if it could be loading from some hidden cache or history, but the old values couldn’t be found anywhere. I even manually checked the .env file with Notepad to make sure it was updated.
What could be the cause of this problem? It’s one of the strangest issues I’ve ever encountered, and at this point, I’m more interested in understanding the root of the problem than just solving it.
I’ve detailed everything in a video (1min).
youtu.be/i3hCg7O66Es