Problem
This one if a fairly common issue that shows up among new Laravel developers.
A lot of people have been trying to update their .env file just to see that when they refresh their application, the .env config values are not showing up.
And what’s more interesting is that quite often, this will show up after you upgrade your Laravel version.
So for example, you would have this in your .env file:
DB_USERNAME=laraveluser
Then naturally, inside the config/database.php file you would have this:
‘database’ => env(‘DB_DATABASE’, ‘lar’),
‘username’ => env(‘DB_USERNAME’, ‘lar’),
]
forge here stands for default values. This means that these values will be used in case you haven’t provided your DB_DATABASE and DB_USERNAME values inside your .env file. This information will come helpful in a bit.
Now, how you know you’ve got this specific problem is if you run into the following exception:
(using password: NO)
This clearly shows that your Laravel application is pulling in the default value from your config/database.php file and not from your .env .
Before doing anything else, you can also try to verify this issue usingphp artisan tinker as well:
=> null
>>> getenv(‘DB_DATABASE’)
=> false
>>> config(‘database.connections.mysql.database’)
=> “lar”
>>> dd($_ENV)
[]
You will often see that doing the following thing won’t make a difference either. However, developers try to test their configuration by running a completely new Laravel install and just copying the old app folder. They don’t run any composer package installations or do anything else.
Unless you are on Linux (I will tell you why a bit later), you won’t see any changes.
Solution
As always, there are many things you can do to try to solve this problem but a few of them are more likely to succeed.
Clear your config cache
First off, if you haven’t, you must clear your config cache before you can do anything else.
Because our code is using so many libraries nowadays, we are forced to cache things to make quick access to them during runtime.
I personally had issues with this on Windows and Mac, however, I noticed that for some reason, on Linux (Ubuntu) these config files are either cleared with some sort of hook or they are not being cached at all, because I was able to reload the application with new .env values without doing the following.
Make sure you clear your config cache by running the following commands:
php artisan config:clear
Check for empty spaces within your .env file
The very next solution is in terms of having white spaces inside your .env file. And this one if where most people fail and start ripping their hair.
It is very easy to miss this but as an example what you could have in your .env file is something like this:
This alone won’t work since white spaces will corrupt the .env file.
What you need to do is to wrap your values inside the quotes like this:
This time everything should work just fine.
Just make sure to clear your config cache every time you make a change. We can do this the same as before:
php artisan config:clear