agyanadda@gmail.com

Laravel Configuration and .env

Laravel Basics 1 min read Updated Dec 2, 2026 Topic 8 of 31

Laravel keeps settings in two places: the .env file and the config folder.

The .env file

.env holds values that differ between your computer and the live server:

  • APP_NAME, APP_ENV (local or production), APP_URL
  • APP_DEBUG — true shows detailed error pages; always false on a live site, because error pages can reveal passwords and code
  • APP_KEY — used for encryption; generate it with php artisan key:generate
  • DB_*, MAIL_*, CACHE_*, QUEUE_* settings

Config files

Files in config/ (such as config/app.php, config/database.php) read .env values with env('KEY', 'default').

Reading settings in your code

Use config('app.name') — the file name, then the key. Call env() only inside config files: after php artisan config:cache, env() used elsewhere returns null.

Your own settings

Add a key to a config file, or create a new one like config/site.php, and read it with config('site.whatsapp').

Caching config

On the live server run php artisan config:cache for speed. After changing .env, run it again (or config:clear).

Example

Example — A custom config file PHP
<?php
// .env
// SITE_WHATSAPP=919876543210

// config/site.php
return [
    'whatsapp' => env('SITE_WHATSAPP', ''),
    'per_page' => 12,
];

// Anywhere in your app (controller, Blade, job):
$number  = config('site.whatsapp');   // 919876543210
$perPage = config('site.per_page');   // 12

// Blade: <a href="https://wa.me/{{ config('site.whatsapp') }}">Chat</a>

env() is used only in the config file; the rest of the app calls config(), which keeps working after php artisan config:cache.

Test Yourself With Exercises

Exercise 1 Multiple Choice

What should APP_DEBUG be on a live website?

Exercise 2 True / False

It is safe to call env() directly in controllers after running config:cache.

Exercise 3 Fill in the Blank

Read the app name with ___('app.name').

Frequently Asked Questions

The config is probably cached. Run php artisan config:clear (or config:cache again on production). If you use php artisan serve, restart it as well.