Laravel environment and config
Use when working with environment variables, .env files and configuration in a Laravel app - adding a new setting, APP_KEY, database/mail/queue credentials, config caching, secrets in CI/CD or Docker, or debugging "env() returns null in production". Keeps secrets out of Git and config reads cache-safe.
Licence MIT
Source delaparra-original
Download zipLaravel environment and config
Settings that differ per environment (local, staging, production) or that are secret live in .env. Code reads them through config(), never directly through env().
Rules
.envis never committed. It must be in.gitignore. Commit.env.examplewith every key and safe placeholder values.- Only call
env()insideconfig/*.phpfiles. Everywhere else useconfig('services.mollie.key'). Afterphp artisan config:cache(which production should run),env()returnsnulloutside config files. - Every new variable is added in three places:
.env.example(placeholder), the relevantconfig/*.phpfile (with a sensible default), and the deployment's secret store (server.env, Forge/Cloud environment, GitHub Actions secrets, Docker secrets). - Never log or print secrets. Don't put them in exceptions,
dd(), or error pages. Production must haveAPP_DEBUG=false. - APP_KEY is generated once per environment with
php artisan key:generateand must never change on a live app (it breaks encrypted data and sessions). Don't share it between environments.
Adding a setting: example
.env.example
MOLLIE_KEY=
config/services.php
'mollie' => [
'key' => env('MOLLIE_KEY'),
],
Usage
$key = config('services.mollie.key');
Common keys to check
APP_ENV, APP_DEBUG, APP_URL (wrong value breaks links, Vite and signed URLs), DB_*, CACHE_STORE, QUEUE_CONNECTION, SESSION_DRIVER, SESSION_DOMAIN, SESSION_SECURE_COOKIE, MAIL_*, LOG_CHANNEL.
Changing config in production
php artisan config:clear # after editing .env on the server
php artisan config:cache # rebuild cache
php artisan queue:restart # workers keep old config until restarted
Docker and CI
- Docker Compose:
env_file: .envorenvironment:; don't bake secrets into images withENVorARG. - CI (GitHub Actions): store values as repository/environment secrets and write them at runtime; for tests use
.env.testingorphpunit.xml<env>entries with fake values.
If a secret was committed
Treat it as leaked: rotate the key at the provider first, then remove it from the repo. Removing it from history alone is not enough.
Files
LICENSESKILL.mdSOURCE.md
Related terms
Installing an agent skill in Claude Code, Codex, Cursor and Copilot