The Fatal Mistake: Calling env() Outside Config Files
Once php artisan config:cache runs in production, the .env file is no longer parsed, and all calls to env() outside config/*.php return null!
Always define configuration values in config/ and access them via config():
PHP
// config/services.php
'github' => [
'token' => env('GITHUB_API_TOKEN'),
],
// In your application code:
$token = config('services.github.token');Production Optimization
In production pipelines, always cache your route and configuration files:
Bash
php artisan config:cache
php artisan route:cache
php artisan view:cache