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.

shareimprove this question
 
   
Are you sure the .env file is named only .env? Not .env.example? – James Elliott Dec 22 '15 at 16:54
   
Andrew are you editing the .env file by the forge interface or uploading it? – Mark Davidson Dec 22 '15 at 16:54
   
@JamesElliott yes it is .env – andrewtweber Dec 22 '15 at 16:55
   
Or are you running this locally? If your running under artisan serve you need to restart it just in case that might be the case. – Mark Davidson Dec 22 '15 at 16:55
   
@MarkDavidson editing it through vi in the command line, and no it's on a full server – andrewtweber Dec 22 '15 at 16:55

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

shareimprove this answer
 
4 
php artisan config:cache solved it for me - nothing else had helped. Not even php artisan config:clear - thanks! – kair May 15 '16 at 20:26
2 
Laravel 5.3 php artisan config:clear is the only thing that worked for me – Dainis Abols Jul 6 at 12:15
1 
Laravel 5.4 - deployed on a VPS: php artisan config:clear worked for me. I didn't do config:cache before so it seemed a bit weird that I had to clear the cache. – Antonio Jul 23 at 21:22

Wow. Good grief. It's because I had an env value with a space in it, not surrounded by quotes

This

SITE_NAME=My website

Changed to this

SITE_NAME="My website"

Fixed it. I think this had to do with Laravel 5.2 now upgrading vlucas/phpdotenv from 1.1.1 to 2.1.0

shareimprove this answer
 
   
Same think - seems like spaces causes issues in .env under 5.2 where as in 5.1 it didn't. – thomthom Dec 29 '15 at 17:59
3 
And this is one of my (many) bugbears with Laravel: it has a habit of sometimes silently ignoring and accepting a variety of configuration and/or other types of errors, with no indication anything has gone wrong. Given, this may be a "feature" of phpdotenv, but Laravel still knows there's a .env file that needs to be included, and that it wasn't successful, but doesn't bother to tell anyone. – dKen Apr 22 '16 at 14:07

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;
shareimprove this answer
 

I had a similar issue in my config/services.php and I solved using config clear and optimizecommands:

php artisan config:clear
php artisan optimize
shareimprove this answer
 

I missed this in the upgrade instructions:

Add an env configuration option to your app.php configuration file that looks like the following:'env' => env('APP_ENV', 'production')

Adding this line got the local .env file to be read in correctly.

shareimprove this answer
 
   
I guess I got the same error, since my app was Laravel 4, then upgraded to 5.0, then to 5.1, etc. Probably missed adding this value in my app.php. Thanks for saving me a lot of time. Here's an upvote! – Bruno P. Kinoshita Jun 2 '16 at 7:56

Also additional to what @andrewtweber suggested make sure that you don't have spaces between the KEY= and the value unless it is between quotes

.env file e.g.:

...
SITE_NAME= My website
MAIL_PORT= 587
MAIL_FROM_NAME= websitename
...

to:

...
SITE_NAME="My website"
MAIL_PORT=587
MAIL_FROM_NAME=websitename
...
shareimprove this answer
 

A temp solution for me was to remove one dot from the $environmentFile = '..env'; to .env in vendor/laravel/framework/src/Illuminate/Foundation/Application.php

I know framework core files are not supposed to be modified, but this to highlight that Laravel sometimes not able to read .env file where there are two dots. Perhaps, its system specific.

shareimprove this answer
 
   
There's already been an answer accepted here, so recommending to edit core files is probably not a good suggestion. – thebluefox Dec 21 '16 at 11:35
   
Accepted answer didn't work for me, and I didn't mention not to modify core files of course. This is just to people know that something like this exists. – Mehar Singh Dec 22 '16 at 4:15

来自  https://stackoverflow.com/questions/34420761/laravel-5-2-not-reading-env-file



Laravel 5 Environment Detection Not Working

PUBLISHED 2 YEARS AGO BY BREDA

Hey guys! I'am having troubles with detecting environments with Laravel 5.0.18. It's just like that Laravel isn't picking up the environment name I'am putting in the APP_KEY. Example : I have this in my .env file : APP_ENV=staging

But when I do

Route::get('/', function() { dd(env('APP_ENV')); });
// OR
Route::get('/', function() { dd(App::environment()); });

It always gives me local !

I'am really confused here! Any ideas ?

Best Answer(As Selected By breda)
pmall

@breda

Just serving the application with artisan serve.

When using php dev server, make sure you restart the server when you change the env file. I almost gone crazy the other day figuring this out.

michaeldyrynda

Are you putting staging into APP_KEY or APP_ENV?

What happens if you try dd(App::environment())?

 
breda

No, I'am putting staging in APP_ENV, not in APP_KEY...

And Like I said, both dd(App::environment()) & dd(env('APP_ENV')) return local, while should be returning staging!

 
devinfd
 devinfd
2 years ago(34,060 XP)

Are you using Holmestead?

 
Kemito
 Kemito
2 years ago(35,400 XP)

Can you please show us .env file (make sure you remove all important information, passwords, api keys)

 
breda

@devinfd Nope. Not using Homestead. Just serving the application with artisan serve.

@Kemito You got it! :)

APP_ENV=staging
APP_DEBUG=true
APP_KEY=RANDOM_STRING

DB_HOST=localhost
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
 
Kemito
 Kemito
2 years ago(35,400 XP)

For debug purpose please make test route. In that route just

\Log::info($_SERVER);

and check if there is APP_ENV there as well as what is value there. If variable is incorrect there aswell then i guess there is problem with booting. Please check your filename if that is correctly '.env' Is APP_ENV only env variable that doesnt work? Create test variable there "TEST_ME" for example and see log/ouput.

Hope this helps.

 
pmall
 pmall
2 years ago(577,195 XP)

@breda

Just serving the application with artisan serve.

When using php dev server, make sure you restart the server when you change the env file. I almost gone crazy the other day figuring this out.

 
breda

Yeah @pmall ! I guess I did what you did! Gone crazy because of this! lol !

Thank you! As well as @Kemito ! Very appreciated :)

 
lukaserat

I am experiencing the same issue. Sharing what I did, hoping it will help. What I did is I remove the bootstrap/cache/config.php. Then all work out smoothly. This happens when you forgot that you cache your configuration. :) You can do the same using php artisan config:clear

 
olimorris
 olimorris
1 year ago(35,285 XP)

Thanks all. Boy was this driving me crazy.

A quick php artisan config:clear and a restart of Homestead sorted it.

 
ms08

I also have problem same too, When I " echo app()->environment() " , it's local. But when I debug:

$app = Config::get('app.cipher'); print_r($app);

Laravel get config of file app.php outside directory local.

This is in file .env

APP_ENV=local

APP_DEBUG=true

APP_KEY=my_key

.....

What's I am wrong ?

 
mstearne
php artisan config:clear

Worked for me.

 
encodia

I'm using Homestead. I set APP_ENV=staging,

If I run php artisan env:

  • on the host: I got the correct environment ("staging")
  • on the guest: I got the wrong environment ("local")

I tried cache:clear, config:clear, tried to reboot Homestead, but didn't solve. Any idea?

Thank you

 
rob897

Same issue here as well.

 
rob897

If you are using Homestead as your Dev environment, no matter what you put in your your .env file seems to matter as your Homestead.yaml file has a APP_ENV key that seems to override what you use in your environment file.


来自  https://laracasts.com/discuss/channels/general-discussion/laravel-5-environment-detection-not-working

 

.env Not Reading Variables Sometimes

PUBLISHED 2 YEARS AGO BY KAYLAKAZE

 

I've set up a custom variable in my .env file, DISPLAY_TZ, to designate the timezone that the app should display times in. I have the User $tz variable being set to this using getenv('DISPLAY_TZ')?:date_default_timezone_get() and then used by calling setTimezone(Auth::user()->tz) on my Carbon object in the view. Seemingly at random, the times displayed will be in UTC (the date_default value) and not America/New_York (the .env value). I don't think this is a Carbon issue, because when I change it to getenv('DISPLAY_TZ')?:'America/New_York' it's always correct. My app loads many pieces of the page via AJAX, if that could be the issue? Also, seemingly at random, some of my AJAX calls will return my access denied page (I'm using Laraguard) even when I have access to the page and a refresh fixes it. I'm wondering if it's related. I'm using SQLite for my database so I'm not using any database variables from the .env so I wouldn't know if those are also not loading.

trevorg

I have the same issue. I haven't been able to figure out what's causing it. It's frustrating because it's an intermittent issue.

https://laracasts.com/discuss/channels/general-discussion/laravel-intermittently-does-not-pull-from-environment-variables-correctly

 
rmariuzzo

I'm facing this issue as well with Laravel 5.1, any suggestion? Please :)

 
kaylakaze

Have you tried "php artisan config:cache" after changes to the .env file as suggested in that other post?

 
gmehtaster

In my case its not reading any variables. I am trying to print the variable but they are all empty. Its working on a server environment but not in my local. Both environments are in sync. I am using Laravel 5.2

It is extremely frustrating. Anyway we can debug this?

 
skovmand
 skovmand
1 year ago(8,780 XP)

+1 It is VERY annoying and periodic!

 
skovmand
 skovmand
1 year ago(8,780 XP)

I had trouble with config:cache, when it is run, the app (Laravel 5.2) doesn't read files from the .env-file. I ran config:clear, and everything works again.

 
igor_talevski

Stupid suggestion, but did you restart server (Apache, Nginx or php artisan serve) ?

 
rverrips
 rverrips
1 year ago(7,150 XP)

Are you on homestead? I have found that homestead.yaml "global" variables over-ride variables set in .env in the project folder.

 
cshelswell

Has anyone found a solution to this. My 5.2 isn't reading any vars from the .env file. I've tried all the above but no joy. My 5.2 is an upgrade from 5.1 but I did add 'env' => env('APP_ENV', 'production'), to my config/app.php file.

if I run php artisan env on my local box I do get "local" so i'm not sure where it's getting that from.

 
bhuvidya

i am having a similar issue with a 5.2.39 install. using env() calls in my config files works fine. but when i try to use env() in template files it returns the empty string. weird. my only solution was to limit the use of env() to just the config files, and then retrieve the values i need using config('blah.blah').

 
simtrax

I also have this issue. Right now I have a huge issue with one of my sites provisioned on Forge. It connects to the correct DB with data from the .env file. But it's impossible to elsewhere check like getenv('APP_ENV'), it just returns empty. I have checked my cache'd config, and the settings are all there and correct.

This problem is on one of 6 sites on the sam server. All the other 5 sites work great. I have a blade statement like this: @if(getenv('APP_ENV') == 'production') Works great on 5 sites, but not on site number 6.

I'd love to get some help with this.

 
leolam2005

Me too, I have the same issue, using Laragon, Lumen for API.mysite.dev, and Laravel for mysite.dev

this is better in laravel because you can cache config

But in Lumen, there is no php artisan cache:config

In the most serious period, 4 out of 5 api requests are 500...

Really, Really serious problem...

 
chileno

+1 having same issue, out of nowhere. trying to figure out the problem.

 
bikerboy

A few things to bear in mind:

  1. Laravel recommends only to use env() within the config files. Use the config() helper in your code instead of env(). For example you can call config('APP_ENV') in your code.
  2. When you use php artisan config:cache all the configuration strings are cached by the framework and any changes you make to your .env file will not be active until you run the php artisan config:cache command again.
来自  https://laracasts.com/discuss/channels/general-discussion/env-not-reading-variables-sometimes?page=1


I had the same issue and realized what bikerboy is trying to say is that you should only directly call you .env variables in you config directory and not elsewhere in your code.

So for instance if you want to use CUSTOM_VARIABLE from your .env file in a view:

  • You should assign it to a config entry first. for example in de config/app.php :
    'customvariable' => env( 'CUSTOM_VARIABLE', 'default custom variable' ),
  • then in the view use config('app.customvariable')
  • optional : After this run the php artisan config:cache

This solved the problem for me where onlye the artisan config:cache did not solve the problem. Replacing env with config in my views did not solve the problem unfortunately.

 
leolam2005

@pluzz okay... again, what if I use Lumen for api which doesn't have config file at root or cache?

I wonder if getenv() <<-- yes, the native php get envirnoment would help.

It appears fewer 500 error but still... there it is

 
kieranjfahy

I'm using Laravel 5.3 and ran into the same issue with ajax requests. As @pluzz mentioned you need to have a config file that references the .env file.

'connection' => [
    'account' => env('ACCOUNT', ''),
    'username' => env('USERNAME', ''),
    'password' => env('PASSWORD', '')
],

Then simply run php artisan config:clear

@StormShadow as talked about this: here: https://laracasts.com/discuss/channels/laravel/configcache-makes-variables-in-env-null

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.

 
CrushCandy

You are suppose to use config cache, this is because the file locks on read, stopping other variables from getting a value.

You never use .env in production, always always config.

 
komirad

@CrushCandy

You never use .env in production, always always config.

Can't be true. envoyer.io supports .env files in production. You also don't commit credentials into repository.

 
WebKenth

Just wondering, why are you storing App specific information within your environment file?

Specifically why are you trying to store the timezone when you can already call config('app.timezone')? which is where your App specific configuration should lie

 
Bebbolus

So you are supposing that if I have several Docker with my app that run in several countries I can't edit only a .env variable based on the location of docker startup (and other config like central server and queue server)?

 
simzal
 simzal
4 months ago(32,890 XP)

It seems like there is some problem with caching of .env files in apache or PHP. I've had an error where one Laravel app got served .env file of another Laravel app. It caused an error of "table X.Y doesn't exist" - where X was db used by the other app and Y the table in the current app. Edit (it's a bug that PHP won't fix): https://github.com/vlucas/phpdotenv/issues/160