Config and env
env() helper
The env file at the project root holds values that change per machine, such as debug flags and third party keys. The env helper reads one value with an optional fallback.
Only configuration files should call env directly, so the rest of the code reads settings through the config helper from the next section.
$debug = env("APP_DEBUG", false); $key = env("API_KEY", "none");
config() helper
The config helper reads merged settings by dotted name: file, dot, key. Files in the config folder provide the defaults, and matching env values override them.
The same helper writes a runtime value when given an array, which is handy inside tests.
$name = config("app.name"); config(["app.timezone" => "UTC"]);
Config cache
On a live server Laravel can compile all configuration files into one fast cache file. Build it with the canonical cache command after every deploy that changes settings.
php artisan config:cache
INFO Configuration cached successfully.
While the cache exists, edits to env values stay invisible until the cache is rebuilt, which surprises many beginners on shared hosting.
Never commit secrets
Secret values belong in the env file on each machine, never in committed code or chat logs. The repository keeps only an example file with empty values that each developer copies locally.
Rotate any key that ever reaches a public place, because history keeps copies long after the file is fixed.
APP_NAME="Demo blog" APP_DEBUG=false API_KEY=""
Next: Artisan commands
Article author: Arthur Isaev