MWZ

MINDWAREZONE

Environment Configuration (.env)

Think of the .env file as the settings file of your Laravel application.

Just like your phone has settings for Wi-Fi, language, and notifications, Laravel uses the .env file to store important settings such as:

  • Application name
  • Database connection details
  • Email configuration
  • Cache settings
  • API keys
  • Debug mode

The .env file is located in the root folder of your Laravel project.

project/
     ├── app/
     ├── config/
     ├── routes/
     ├── storage/
     ├── .env
     └── artisan
            

Why Do We Use the .env File?

Imagine you have two versions of your application:

Development Environment

Your local computer.

APP_ENV=local
APP_DEBUG=true
            

Production Environment

Live server used by real users.

APP_ENV=production
APP_DEBUG=false
            

Instead of changing code every time, you simply change values in the .env file.

Basic Example of a .env File

APP_NAME="My Laravel App"
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxx
APP_DEBUG=true
APP_URL=http://localhost
            

Laravel automatically reads these values when the application starts.

Important Rules

Rule 1: Never Share Your .env File

The .env file contains sensitive information like:

DB_PASSWORD=mysecretpassword
MAIL_PASSWORD=secret123
API_KEY=abcdef123456
            

For this reason, Laravel includes .env in .gitignore.

Never upload it to GitHub.

Rule 2: One Environment = One .env File

Each environment can have different values.

Local:

DB_DATABASE=laravel_local
            

Production:

DB_DATABASE=laravel_production
            

Common Laravel 13 Environment Variables

1. APP_NAME

Defines the application name.

APP_NAME="Online Shop"
            

Usage:

config('app.name')
            

Output:

Online Shop

2. APP_ENV

Specifies where the application is running.

APP_ENV=local
            

Common values:

local
development
testing
staging
production
            

Example:

app()->environment()
            

Output:

local

3. APP_KEY

Used for encryption and security.

APP_KEY=base64:xxxxxxxxxxxxxxxx
            

Generate it using:

php artisan key:generate
            

Without an APP_KEY, Laravel security features will not work properly.

4. APP_DEBUG

Controls error visibility.

Development:

APP_DEBUG=true
            

Production:

APP_DEBUG=false
            

When TRUE

Laravel shows detailed error messages.

SQL ErrorFile NameLine NumberStack Trace

When FALSE

Users see a generic error page.

Server Error

Always set it to false in production.

5. APP_URL

Your application's main URL.

Local:

APP_URL=http://localhost
            

Production:

APP_URL=https://example.com
            

Laravel uses this for generating links, emails, and asset URLs.

Database Configuration

These settings connect Laravel to your database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=password
            

Meaning

Variable Description
DB_CONNECTION Database type
DB_HOST Database server
DB_PORT Database port
DB_DATABASE Database name
DB_USERNAME Username
DB_PASSWORD Password

Example

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shop_db
DB_USERNAME=root
DB_PASSWORD=123456
            

Laravel will use these details whenever it communicates with MySQL.

Cache Configuration

Cache stores frequently used data for faster performance.

CACHE_STORE=database
            

Other options:

CACHE_STORE=file
CACHE_STORE=redis
CACHE_STORE=database
            

Session Configuration

Sessions store user information temporarily.

SESSION_DRIVER=database
            

Common options:

SESSION_DRIVER=file
SESSION_DRIVER=database
SESSION_DRIVER=redis
            

Queue Configuration

Queues help run background tasks.

QUEUE_CONNECTION=database
            

Examples:

QUEUE_CONNECTION=sync
QUEUE_CONNECTION=database
QUEUE_CONNECTION=redis
            

Mail Configuration

Used for sending emails.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
            

Example use:

Mail::to($user)->send(new WelcomeMail());
            

Laravel reads the SMTP details from .env.

Redis Configuration

Redis is used for caching, queues, and sessions.

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
            

How Laravel Accesses .env Values

Inside configuration files:

env('APP_NAME')
            

Example:

'name' => env('APP_NAME', 'Laravel'),
            

How to Access Values in Your Application

Recommended approach:

config('app.name')
            

Example:

echo config('app.name');
            

Output:

Online Shop

Configuration Caching

Laravel can cache all configuration values for better performance.

Create cache:

php artisan config:cache
            

Clear cache:

php artisan config:clear
            

View cached configuration:

php artisan optimize
            

In production, configuration caching is highly recommended.

Best Practices

✅ Keep Secrets in .env

STRIPE_SECRET=xxxxx
GOOGLE_API_KEY=xxxxx
            

✅ Use Different Files for Different Environments

Local:

APP_DEBUG=true
            

Production:

APP_DEBUG=false
            

✅ Cache Configuration in Production

php artisan config:cache
            

✅ Never Commit .env to Git

.env

should remain inside .gitignore.

✅ Use config() Instead of env()

Good:

config('app.name')
            

Avoid:

env('APP_NAME')
            

outside configuration files.

Summary

The .env file in Laravel 13 is the application's settings and secrets storage. It contains:

  • Application settings (APP_NAME, APP_ENV)
  • Security key (APP_KEY)
  • Debug mode (APP_DEBUG)
  • Database credentials
  • Mail settings
  • Cache settings
  • Queue settings
  • Third-party API keys

Laravel loads these values automatically when the application starts, making it easy to manage different environments without changing application code.