When developing locally with Laravel, you might encounter these frustrating problems:
- You change the
.env
file, but the changes don't take effect - Your app unexpectedly connects to the wrong environment (database, API, etc.)
- Configuration values seem stale and outdated
Most often, these issues are caused by configuration caching.
How .env
and Configuration Caching Work
Laravel uses environment variables defined in .env
inside its config files under config/*.php
. To improve performance, especially in production, Laravel allows you to cache all your configuration and environment variables into a single file.
Running php artisan optimize
creates this cache.
Specifically, config::cache
compiles .env
values into bootstrap/cache/config.php
, so changes to .env
won't be picked up until the cache is cleared.
Location and Contents of Cache Files
Configuration caches are stored in the bootstrap/cache/
directory.
After running php artisan optimize
, you'll typically see files like:
bootstrap/cache/config.php ← configuration cache
bootstrap/cache/routes-v7.php ← route cache
bootstrap/cache/packages.php ← package cache
Opening config.php
reveals an array with all .env
values expanded and saved.
As long as this cache exists, Laravel won't reload your .env
file directly.
How to Avoid Cache Issues
If you want .env
changes to apply immediately, clear the cache:
php artisan config:clear
php artisan cache:clear
A Tool I Built to Check .env
vs Cache Differences
I personally got stuck many times wondering "Why am I connecting to the wrong environment?" To help, I created a CLI tool that easily compares your .env
file and the cached config.php
to highlight differences.
Note on Artisan Commands and Current Limitations
I wasn't aware that custom Artisan commands could be created, so I implemented this tool as a standalone Rust CLI.
Also, there are some cases that the current implementation does not yet cover, including:
- Environment variable expansion (e.g.,
MAIL_FROM_NAME="${APP_NAME}"
) - Values present in
$_ENV
but not in the.env
file -
.env.[environment]
files for environment-specific configuration - Non-local file systems
How to Install
git clone git@github.com:tommykw/laravel-env-diff.git
cd laravel-env-diff
cargo build --release
cargo install --path .
How to Run
After installation, run the tool from the root directory of your Laravel project:
laravel-env-diff
Sample Output
=== Differences between .env and bootstrap/cache/config.php ===
[DIFF] DB_HOST
[DIFF] REDIS_PASSWORD
This output shows which environment variable keys differ between your .env
and the cached config.
It helps you quickly identify what hasn't been reflected in the cache.
Summary
-
.env
values get cached intobootstrap/cache/config.php
byconfig:cache
- If
.env
changes don't take effect, clear the cache with artisan commands
Understanding .env
caching saves you a lot of debugging time.
Top comments (2)
I don't understand why you created it in Rust instead as an artisan command? it probably is faster but If you want to run it on a production server the Rust dependency can be a hurdle.
There are also a few cases I can think of that are not met by the current code;
MAIL_FROM_NAME="${APP_NAME}"
env
function reads the (dotEnv library extended)$_ENV
variable, this means the key doesn't need to be in the .env fileThank you for your comment.
I actually didn’t know that you could create custom Artisan commands.
As you mentioned, implementing it as an Artisan command would be better.
Also, I missed supporting the cases you pointed out.