#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Environment Configuration (.env file)

Every Laravel application needs settings that change depending on where it's running — a local database on your laptop looks nothing like a production database in the cloud. Laravel manages this through a single file: .env.

Purpose of .env

The .env file stores environment-specific configuration as simple key-value pairs, kept separate from the application's core code. Laravel's configuration files in config/ read values from .env at runtime, so the same codebase can behave differently in local, staging, and production environments without any code changes.

Common Variables

VariablePurpose
APP_ENVIdentifies the current environment (local, staging, production)
APP_KEYA unique encryption key used to secure sessions and data
APP_DEBUGToggles detailed error output, disabled in production
DB_CONNECTIONSpecifies the database driver, such as mysql or pgsql
DB_HOST / DB_DATABASEDefines where and which database to connect to
MAIL_MAILERSets the driver used for sending outgoing email

Best Practices

Once the environment is configured correctly, the application is ready to actually respond to requests — which starts with defining routes in web.php and api.php.

How Laravel Loads the File

Understanding the loading order explains most .env surprises:

  1. On boot, Laravel uses the phpdotenv library to read .env from the project root.
  2. If a file such as .env.testing matches the current APP_ENV, Laravel loads that one instead. PHPUnit and Pest runs use this to point tests at a separate database.
  3. Real server environment variables take priority over values in the file, so hosting platforms can inject secrets without a file at all.
  4. Files in config/ call env() to read the values and store them in the configuration repository.
  5. The rest of the application reads settings with config().

Reading Values the Right Way

Add a new setting in two places. First the .env file:

APP_NAME="Shop Admin"
PAYMENT_API_URL=https://sandbox.example-payments.test
PAYMENT_API_KEY=replace-me
MAIL_FROM_NAME="${APP_NAME}"

Values with spaces need quotes, and ${APP_NAME} reuses another variable. Then expose it through a config file such as config/services.php:

'payment' => [
    'url' => env('PAYMENT_API_URL', 'https://sandbox.example-payments.test'),
    'key' => env('PAYMENT_API_KEY'),
],

Finally, read it anywhere in the app:

$url = config('services.payment.url');

if (app()->environment('production')) {
    // production-only behaviour
}

The second argument to env() is a default used when the variable is missing.

Why env() Belongs Only in Config Files

In production you should run php artisan config:cache. It merges all config files into one cached file for faster boots. After caching, the .env file is no longer loaded, so any env() call inside a controller, model or view returns null. Code that worked locally then breaks on the server. Keeping env() inside config/ and using config() everywhere else avoids this completely.

Useful commands while working with environments:

php artisan key:generate      # create APP_KEY for a fresh install
php artisan config:clear      # drop the cached config after edits
php artisan about             # show current environment and cache status
php artisan env:encrypt       # create an encrypted copy of .env

The encrypted copy can be committed safely, while the decryption key is stored in your deployment platform's secret store.

Errors Developers Commonly Hit

  • Edits not taking effect: the config is cached; run config:clear or rebuild the cache.
  • "No application encryption key": APP_KEY is empty; generate one.
  • Broken values: an unquoted value with spaces or a # character gets cut off.
  • Tests wiping real data: create .env.testing with its own database before running tests.
  • Exposed file: make sure the web server's document root is the public/ folder, so .env can never be downloaded.

Checklist: template in .env.example, secrets only in .env or server variables, env() only in config, config cached on deploy, and debug turned off in production.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course