Search code examples
databaselaravellaravel-4environment-variableslaravel-forge

Laravel 4 with forge, environment variables issue


I'm having trouble using forge and environment variables. When I use getenv('DB_NAME') and so further I get connection problem when trying to migrate. When I var_dump the values in BaseController.php I've the right values. But still not working. If I instead write the values from the given var_dump in the database.php config file I get it working. But why the hell does it not work with getenv when the values are correct, any ideas?

In my BaseController.php:

var_dump(getenv('DB_HOST'));
var_dump(getenv('DB_NAME'));
var_dump(getenv('DB_USERNAME'));
var_dump(getenv('DB_PASSWORD'));

In my database.php:

'host'      => getenv('DB_HOST'),
'database'  => getenv('DB_NAME'),
'username'  => getenv('DB_USERNAME'),
'password'  => getenv('DB_PASSWORD'),

With this code I get the error message:

[PDOException]                                                                     
SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO) 

If I instead write the values I get from my var_dumps in BaseController.php everything works fine. Any ideas?


Solution

  • Make sure you're env file is called .env.php and not .env.production.php. I made this mistake, and although having .env.production.php will work in app, it won't when using Artisan.

    When creating env vars within Forge, if it's for a production environment, be sure to leave the environment field blank, as opposed to explicitly setting it to production.