问题
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