Configuration
Laravel 8
Laravel 9
Laravel 10
Laravel 11
Laravel 12
Laravel .env File Not Loading - How to Fix
This issue occurs when Laravel doesn't read values from the .env file, returning null for environment variables.
The Error
Error Message
Environment variables not loading / .env values null
Common Causes
- 1 Configuration cached with old values
- 2 .env file not in project root
- 3 Syntax error in .env file
- 4 Spaces around = in .env values
- 5 Using env() in config files after caching
Solutions
1
Clear all caches
Bash
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
2
Check .env syntax - no spaces around =
ENV
# Correct
APP_NAME="My App"
DB_PASSWORD=secret123
# Wrong (has spaces)
APP_NAME = My App
DB_PASSWORD = secret123
3
Use config() instead of env() in code
PHP
// Wrong - env() returns null after config:cache
$name = env('APP_NAME');
// Correct - use config helper
$name = config('app.name');
4
Quote values with special characters
ENV
APP_NAME="My App's Name"
DB_PASSWORD="pass#word!123"
MAIL_FROM_NAME="${APP_NAME}"
Need Help With Your Laravel Project?
I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.
Currently available for 2-3 new projects
About Hafiz
Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.
View Portfolio