3

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?

5
  • 1
    Is your file at app/config/local/database.php? Commented Aug 15, 2013 at 19:44
  • yes correct. if i migrate and use env=local it picks that up! Commented Aug 15, 2013 at 19:54
  • 1
    Where are you making db calls? If in CLI, you need to specify --env=local. Also, considering making your default your local environment and specifying production specially, just so you don't default to production "by accident" ever (since that could, obviously, lead to terrible things happening). Commented Aug 15, 2013 at 20:10
  • 1
    Won't $myenv = getenv('APPLICATION_ENV') always be true? You're assigning the result of getenv to $myenv. Commented Jun 19, 2014 at 5:36
  • Not if 'APPLICATION_ENV' has not been set. Then it would be false and default to local. Commented Jun 20, 2014 at 4:54

4 Answers 4

2

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.

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

1 Comment

That was my problem, Thank you for posting the solution one year earlier :)
1

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

Comments

1

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.

Comments

1

Clean the config cache as it may affect

php artisan config:cache

Comments

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.