Laravel: .env changes ignored / env('X') returns null after php artisan config:cache
After config:cache, Laravel no longer reads .env, so env() calls outside config files return null and edits to .env have no effect.
env() returns null after config:cacheLaravel .env changes not working.env not being read Laravel config cachedenv('APP_DEBUG') returns nullLaravel changes to .env file not taking effect Meaning
When config is cached, .env isn't loaded; only values compiled into config/*.php exist. Use config('app.x') in app code and call env() only inside config files. After changing .env run php artisan config:clear (or config:cache again).
Common causes
env()used in controllers/models instead ofconfig().envedited but config cache not rebuilt- Different
.envon the server - Cache stored from another environment
config/*.phpmissing the key
⚡ Quick fix
- Read values via
config('services.key')and define them in config files php artisan config:clearafter editing .env- Rebuild caches on deploy:
php artisan config:cache
Detailed fix by platform
Laravel
php artisan optimize:clearclears config, route, view and event caches
How to diagnose
- Cached — bootstrap/cache/config.php exists?
- Usage — env() outside config?
- Deploy — Cache step?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- 503 Service Unavailable (maintenance mode stuck) Laravel: Be right back. / 503 Service Unavailable after php artisan down
- Attempt to read property "x" on null Laravel/PHP 8: ErrorException: Attempt to read property "name" on null
- Can't connect to Redis from another host (bind 127.0.0.1) Redis: Could not connect to Redis at 10.0.0.5:6379: Connection timed out
Most viewed in Laravel
- MassAssignmentException Laravel: Illuminate\Database\Eloquent\MassAssignmentException: Add [x] to fillable property to allow mass assignment
- The GET method is not supported for route Laravel: The GET method is not supported for route x. Supported methods: POST (MethodNotAllowedHttpException)
- 403 This action is unauthorized Laravel: Illuminate\Auth\Access\AuthorizationException: This action is unauthorized.
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 4 Nov 2026