Reloading .env variables without restarting server (Laravel 5, shared hosting)

前端 未结 7 2220
旧巷少年郎
旧巷少年郎 2020-12-23 13:46

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         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 14:39

    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.

提交回复
热议问题