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

bazingo's avatar

env() / getenv() Sometimes Not Initialized

I'm getting a seemingly random 500 error getting logged as "Database hosts array is empty" in the error logs.

I'm seeing this maybe once out of 300 requests (guess). Repeating the request is successful. Other requests initiated at the same time are also successful. So I will have 4/5 requests work and one fail with the aforementioned error.

I am working with a config/database.php with 5 connections specified. The bulk of the connection details are pulled from .env via getenv. I received this in both Lumen 7 and now Lumen 8. I'm at a bit of a loss as to why this is randomly occurring. It has occurred on multiple boxes talking to different databases.

Searching around indicated this is usually related to configuration issues, but I'm not sure why it would work the majority of the time. Are there any particular PHP or Apache configuration parameters I should look out for with this?

Any help would be appreciated.

0 likes
4 replies
franklyt's avatar

Having similar issues (in Laravel, not Lumen) -- the very low failure-rate makes it difficult to reproduce and debug.

Long shot: Did you ever make progress on this?

vatsake's avatar

Sorry to dig up the grave, but have any of you found the solution?

BTW, are you both running laravel on windows as well?

Sinnbeck's avatar

@vatsake why not make a thread with a description of the problem so others can try helping you as well

StanleyPeters's avatar

I was also getting a Database hosts array is empty. Error. Solved it by editing config/database.php and changing case to upper for multiple env keys that were entered with mixed case. Something to check. Laravel 8

Please or to participate in this conversation.