# 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 zip](<https://delaparra-services.nl/skills/laravel-env-config/download>)   

# Laravel 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

 

1. **` .env ` is never committed.** It must be in ` .gitignore `. Commit ` .env.example ` with every key and safe placeholder values. 
1. **Only call ` env() ` inside ` config/*.php ` files.** Everywhere else use ` config('services.mollie.key') `. After ` php artisan config:cache ` (which production should run), ` env() ` returns ` null ` outside config files. 
1. **Every new variable is added in three places:** ` .env.example ` (placeholder), the relevant ` config/*.php ` file (with a sensible default), and the deployment's secret store (server ` .env `, Forge/Cloud environment, GitHub Actions secrets, Docker secrets). 
1. **Never log or print secrets.** Don't put them in exceptions, ` dd() `, or error pages. Production must have ` APP_DEBUG=false `. 
1. **APP\_KEY** is generated once per environment with ` php artisan key:generate ` and 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: .env ` or ` environment: `; don't bake secrets into images with ` ENV ` or ` ARG `. 
- CI (GitHub Actions): store values as repository/environment secrets and write them at runtime; for tests use ` .env.testing ` or ` phpunit.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

 

- ` LICENSE ` 
- ` SKILL.md ` 
- ` SOURCE.md `

 

## Related terms

 

- [ENV](<https://delaparra-services.nl/knowledge-hub/env>) 
- [environment](<https://delaparra-services.nl/knowledge-hub/environment>) 
- [Identity & Access Management](<https://delaparra-services.nl/knowledge-hub/identity-access-management>) 
- [Secret Management](<https://delaparra-services.nl/knowledge-hub/secret-management>)

 

[Installing an agent skill in Claude Code, Codex, Cursor and Copilot](<https://delaparra-services.nl/manuals/een-skill-installeren-in-je-ai-assistent>)

[HTML version](https://delaparra-services.nl/skills/laravel-env-config)
