Why Laravel uses wrong database file after setting environment?

自闭症网瘾萝莉.ら 提交于 2020-01-04 01:59:09

问题


In bootstrap/start.php I have the following:

$env = $app->detectEnvironment(function()
{
    if($myenv = getenv('APPLICATION_ENV')):
        return $myenv;
    else:
        return 'local';
    endif;
});

Ok so I setup a local folder and put in a database.php file with my local connections.

Just to make sure its picking up the correct environment I put in the template: {{ App::environment(); }} which outputs local.

But when making a DB call its giving me error: Undefined index: DB1_HOST

My base (production) database.php file has:

'host'      => $_SERVER["DB1_HOST"],
'database'  => $_SERVER["DB1_NAME"],
'username'  => $_SERVER["DB1_USER"],
'password'  => $_SERVER["DB1_PASS"],

Why is it looking at the production database file?


回答1:


Laravel also stores the config information in

bootstrap/cache/config.php

In some cases it's not updated which may result in wrong database information. Deleting the file should resolve the issue.




回答2:


If you are trying to use artisan in your terminal and you want to set the environment variable once and for all, you can do :

export APPLICATION_ENV=local

And check you current environment using php artisan env




回答3:


Production config files are loaded first and then merged with overrides from other environments. If the production file generates an error (e.g. undefined index) the config loading will bail early without loading the overrides. In the production config file, check the value is set before attempting to use it and the local config file will then load correctly.




回答4:


Clean the config cache as it may affect

php artisan config:cache



来源:https://stackoverflow.com/questions/18260199/why-laravel-uses-wrong-database-file-after-setting-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!