My Laravel 5 has run OK until the database was configured, then found this error:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o
It's possible that your configuration variables are cached. Verify your config/app.php
as well as your .env
file then try
php artisan cache:clear
on the command line.
If you have run php artisan config:cache
on your server, then your Laravel app could cache outdated config settings that you've put in the .env
file.
Run php artisan config:clear
to fix that.
In case anybody stumbles upon this question who cannot reload their webserver (long running console command like a queue runner) or needs to reload their .env file mid-request, i found a way to properly reload .env variables in laravel 5.
use Dotenv;
use InvalidArgumentException;
try {
Dotenv::makeMutable();
Dotenv::load(app()->environmentPath(), app()->environmentFile());
Dotenv::makeImmutable();
} catch (InvalidArgumentException $e) {
//
}
I know this is old, but for local dev, this is what got things back to a production .env file:
rm bootstrap/cache/config.php
then
php artisan config:cache
php artisan config:clear
php artisan cache:clear
To be clear there are 4 types of caches you can clear depending upon your case.
php artisan cache:clear
You can run the above statement in your console when you wish to clear the application cache. What it does is that this statement clears all caches inside storage\framework\cache.
php artisan route:cache
This clears your route cache. So if you have added a new route or have changed a route controller or action you can use this one to reload the same.
php artisan config:cache
This will clear the caching of the env file and reload it
php artisan view:clear
This will clear the compiled view files of your application.
Most of the shared hosting providers don't provide SSH access to the systems. In such a case you will need to create a route and call the following line as below:
Route::get('/clear-cache', function() {
Artisan::call('cache:clear');
return "All cache cleared";
});
A short solution:
use Dotenv;
with(new Dotenv(app()->environmentPath(), app()->environmentFile()))->overload();
with(new LoadConfiguration())->bootstrap(app());
In my case I needed to re-establish database connection after altering .env programmatically, but it didn't work , If you get into this trouble try this
app('db')->purge($connection->getName());
after reloading .env , that's because Laravel App could have accessed the default connection before and the \Illuminate\Database\DatabaseManager
needs to re-read config parameters.