Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kaylakaze's avatar

.env Not Reading Variables Sometimes

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.

0 likes
48 replies
rmariuzzo's avatar

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

kaylakaze's avatar

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

10 likes
gmehtaster's avatar

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?

1 like
skovmand's avatar

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.

31 likes
igor_talevski's avatar

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

4 likes
rverrips's avatar

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

cshelswell's avatar

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's avatar

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').

5 likes
simtrax's avatar

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's avatar

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's avatar

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

bikerboy's avatar

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.
6 likes
pluzz's avatar

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.

9 likes
leolam2005's avatar

@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's avatar

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.

1 like
lara25260's avatar

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.

jclee100's avatar

@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.

2 likes
WebKenth's avatar

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

1 like
anon4664's avatar

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's avatar

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

phpfalcon's avatar

don't use php artisan serve . that's it

3 likes
Shahrukh4's avatar

When you fired command

 php artisan config:cache 

then it will wipe out all the env variables and env() will give null values, try running follwing command and boom there your env() again begin to catch all env variable

 php artisan config:clear
9 likes
SomeT's avatar

Had issues with this myself recently, I narrowed it down to my ISP being slow when updating live pages, I have ended up using google dns servers to get around this issue. Probably not the solution but might be worth a try.

Shahrukh4's avatar

Sometimes when you changed in your .env file it does not take correct values form that, the problem is due to some configuration cache.Try running following commands, hope will work

php artisan config:cache //flush all cached env variable
php artisan config:clear   //repopulate all the env variable
php artisan cache:clear    //flush all the cached content
1 like
john.pope's avatar

I know this is old, but for local dev, this is what got things back to a live .env file:

rm bootstrap/cache/config.php
1 like
john.pope's avatar

@CRONIX - haha, you're right.. not sure what I was doing the other day. I had thought I had tried that like 20 times, but the file was still there. And when I deleted it it worked (imagine that!). Sorry!

Next

Please or to participate in this conversation.