2

I am trying to load the local version of my database.php file located in my app/config/local directory. I have setup my $env environment detection in start.php for local.

$env = $app->detectEnvironment(array(
'local' => array('your-machine-name')
));

The correct environment appears to be detecting correctly as the result of using:

App::environment('local')

evaluates as TRUE, however it only loads the default database.php in the app/config directory.

Just in case it was still related to detection I have tried just hardcoding local as the environment, but it has the same effect.

$env = $app->detectEnvironment(function()
{
return 'local';
});

I also tried using a different name for the environment in case 'local' had some undocumented reserved meaning.

I have successfully setup environments in other projects using Laravel 4, and I can't work out for the life of me what is different in this case. I'd appreciate any suggestions on what could lead to this behaviour.

The local environment is run on MAMP.

I just did an additional test and found that app.php file in my local folder loaded correctly. It is only the local database.php file that does not appear to be loading.

1
  • I just noticed that there is another unresolved question that seems to mention the same issue: link Commented Jun 17, 2014 at 6:55

2 Answers 2

2

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. Check whether it's generating an error in the log. If so, in the production config file, check the value is set before attempting to use it and the local config file will then load correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is handy to know. My production config file included a reference to a global server variable which was only available on the production server (RDS Database variables on AWS Elastic Beanstalk). Since these were not available in the local and test servers they were throwing the error and then not loading the DB config files with the right details. Thanks for the answer, I had a workaround I have been using, but now I can finally fix the issue.
1

In your project inside app/config directory you should create a directory named local and then in that directory you should create a file named database.php and in that fole you should provide the configuration, for example:

// File: app/config/local/database.php

return array(

'connections' => array(

        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'myTestDb', // myTestDb is a db name for example
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        'pgsql' => array(
            'driver'   => 'pgsql',
            //...
        ),
    ),
);

Then in your project_folder/bootstrap/start.php file use something like this:

$env = $app->detectEnvironment(array(
'local' => array('*.dev', gethostname()),
    'production' => array('*.com', '*.net')
));

You may also check this answer for environment setup. This should work.

4 Comments

I agree this should work, but if you reread my question you will see that the environment detection in start.php appears to be working correctly, and I have already setup my local database.php in my config/local folder. The problem is that the local version of database.php is not being loaded despite the environment being detected as local.
What makes you think that, the local version of your database.php is not being loaded?
Because it tries to connect to the database using the credentials in the database.php file in the config directory rather than those in the config/local directory. At the moment my workaround is to use an if (App::environment('local')){ return array( /* database details */ );} statement in the database.php in the config directory, but I really want to isolate the actually problem and fix it - Thanks for trying to help so far.
now i am having the same issue. upon checking the environment, it is local, it loads other config files from local folder yet, when it comes to database, it doesn't load the configs from local folder but direct from the production env.... and to surprise, it is happening only for the database config. Rest configs are working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.