Interview frequency: …
Short answer
When config:cache runs, it combines all configuration files and stops loading .env, so env() always returns null in application code.
Explanation
php artisan config:cache is an essential production optimization that merges all configuration into one fast cache file. Once enabled, Laravel stops reading .env, so environment variables are no longer loaded and direct env('KEY') calls in Controllers or services fail. Instead, read the environment variable in config/app.php, then call config('app.key') in the application.
Code example
// A common production mistake:
$secret = env("STRIPE_SECRET"); // Returns null when running config:cache!
// The correct approach:
$secret = config("services.stripe.secret");Common mistakes
- Calling env() inside Controllers or Models, breaking the application when production deployment runs the cache command.
Follow-up questions
- What are the main production performance commands (route:cache, view:cache, config:cache)?
- How did Laravel 11 simplify application startup by removing the Http Kernel and moving configuration into bootstrap/app.php?