Environment Configuration (.env)
Laravel uses a .env file to manage environment-specific configuration — database credentials, API keys, debug settings, mail drivers, and cache backends. This approach follows the twelve-factor app methodology: configuration that varies between environments (local, staging, production) should be stored in environment variables, not in code. The .env file is never committed to version control, so each developer and each server can have different settings without changing application code.
The .env File Structure
APP_NAME=MyLaravelApp
APP_ENV=local
APP_KEY=base64:abc123...
APP_DEBUG=true
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=root
DB_PASSWORD=secret
CACHE_DRIVER=redis
SESSION_DRIVER=database
QUEUE_CONNECTION=redis
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
REDIS_HOST=127.0.0.1
REDIS_PORT=6379How Configuration Works in Laravel
- The .env file is read by the vlucas/phpdotenv package and loaded into PHP's environment. Access values with env('DB_HOST', 'default_value')
- Config files in config/ reference .env values: 'host' => env('DB_HOST', '127.0.0.1'). Always use config() helper in your application code, not env() directly — env() only works before config is cached
- In production, run php artisan config:cache — this compiles all config files into a single cached file for performance. After caching, env() calls in your app code will return null — only config() works
- Never store .env in version control. Commit .env.example with placeholder values so new developers know what variables are needed
- APP_DEBUG=true shows detailed error pages (useful in development). In production, always set APP_DEBUG=false to prevent leaking sensitive information in error pages
- APP_KEY is used for encryption (sessions, cookies, passwords). Generate it with php artisan key:generate. If you change it, all existing encrypted data becomes unreadable
Tip
Tip
Practice Environment Configuration env in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Never commit .env. All values are strings.
Practice Task
Note
Practice Task — (1) Write a working example of Environment Configuration env from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Environment Configuration env is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Laravel uses a.
- The .env file is read by the vlucas/phpdotenv package and loaded into PHP's environment. Access values with env('DB_HOST', 'default_value')
- Config files in config/ reference .env values: 'host' => env('DB_HOST', '127.0.0.1'). Always use config() helper in your application code, not env() directly — env() only works before config is cached
- In production, run php artisan config:cache — this compiles all config files into a single cached file for performance. After caching, env() calls in your app code will return null — only config() works