Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 not reading env file

After upgrading to Laravel 5.2, none of my .env file values are being read. I followed the upgrade instructions; none of my config files were changed except auth.php. They were all working fine in previous version, 5.1.19

.env contains values such as

DB_DATABASE=mydb DB_USERNAME=myuser 

config/database.php contains

'mysql' => [     'database' => env('DB_DATABASE', 'forge'),     'username' => env('DB_USERNAME', 'forge'), ] 

I get this error:

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

Clearly not pulling in my env config. This is affecting every single one of my config files, including third party such as bugsnag.

I also tried

php artisan config:clear php artisan cache:clear 

Update

Trying php artisan tinker

>>> env('DB_DATABASE') => null >>> getenv('DB_DATABASE') => false >>> config('database.connections.mysql.database') => "forge" >>> dd($_ENV) [] 

I have tried installing a fresh copy of Laravel 5.2. I basically only copied in my app folder; no additional composer packages are included. Still having the same issue. I have other Laravel 5.2 projects on the same server that are working fine.

like image 941
andrewtweber Avatar asked Dec 22 '15 16:12

andrewtweber


People also ask

Where is .env file in Laravel?

In your main Laravel folder you should have . env file which contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env('KEY'). Don't confuse it with .

How can I get current environment in Laravel?

In Laravel 4 and 5, the Laravel official docs suggest using: $environment = App::environment();


2 Answers

If any of your .env variables contains white space, make sure you wrap them in double-quotes. For example:

SITE_NAME="My website"

Don't forget to clear your cache before testing:

php artisan config:cache php artisan config:clear 
like image 177
benjolly1989 Avatar answered Sep 25 '22 22:09

benjolly1989


From the official Laravel 5.2 Upgrade Notes:

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

Reference: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0

like image 45
Gaurav Gupta Avatar answered Sep 21 '22 22:09

Gaurav Gupta