• 10

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

My Laravel 5 has run OK until the database was configured, then found this error:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

Doing some research it seems that I configured MySQL access too late, so I should restart the server in order to get the correct environment variables. Well, I'm using Dreamhost's shared server and I just can't do that.

How should I fix this issue?


      • 2
    • You don't have to restart webserver in order to make Laravel reread it's .env file. Laravel does it during every run.
      • 1
    • @max.lanin so then if that's not the problem, if the credentials are correct, what is the problem? I'm running php artisan migrate so that I apply the migration

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.

  • 75
Reply Report
      • 1
    • Running php artisan config:cache actually clears the cache and rebuilds it, so I'd recommend that instead of just config:clear if you want to keep using the cache. BUT TIL that if you use the env() function outside of the cache files, you'll get NULL values when your config is cached. So make sure to ONLY use env() inside the files in your cache directory

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


php artisan config:cache
php artisan config:clear
php artisan cache:clear
  • 16
Reply Report
      • 1
    • The docs specifically state "You should typically run the php artisan config:cache command as part of your production deployment routine. The command should not be run during local development"

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.

  • 14
Reply Report
      • 2
    • Thanks but I already tried that. Anyway, if .env variables override config/app.php variables why should I check this file?
    • They don't. The config/app.php file use them with the env() function. All config files do, really. So I'd check the config/database.php to see if the calls to env() use the correct names.
      • 2
    • Let's see, if I have the app working locally with apache I should only tweak some values in the .env file in the server, right? And if nothing is changed inside the config/files everything should be working I guess. There's something I'm missing damn

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.

For Shared Hosting

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() {
    return "All cache cleared";
  • 5
Reply Report

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


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.

  • 4
Reply Report

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::load(app()->environmentPath(), app()->environmentFile());
} catch (InvalidArgumentException $e) {
  • 3
Reply Report

In config/database.php I changed the default DB connection from mysql to sqlite. I deleted the .env file (actually renamed it) and created the sqlite file with touch storage/database.sqlite. The migration worked with sqlite.

Then I switched back the config/database.php default DB connection to mysql and recovered the .env file. The migration worked with mysql.

It doesn't make sense I guess. Maybe was something serverside.

  • -11
Reply Report
      • 1
    • Even though it solved your problem, you shouldn't mark a "hack" as an answer - it encourages bad practices

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions