0
0
Laravelframework~30 mins

Environment configuration in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Laravel Environment Configuration
📖 Scenario: You are building a Laravel application that needs to connect to a database and send emails. To keep your settings safe and flexible, you will use Laravel's environment configuration system.
🎯 Goal: Set up environment variables in Laravel to configure the database connection and mail settings. Then, access these variables in the Laravel configuration files.
📋 What You'll Learn
Create environment variables in the .env file for database and mail settings
Add a configuration variable in config/database.php to use the environment variables
Add a configuration variable in config/mail.php to use the environment variables
Ensure the Laravel app reads the environment variables correctly
💡 Why This Matters
🌍 Real World
Most Laravel applications use environment variables to keep sensitive data like passwords and API keys safe and to easily switch settings between development and production.
💼 Career
Understanding environment configuration is essential for Laravel developers to build secure, maintainable, and flexible applications.
Progress0 / 4 steps
1
Create environment variables in .env
Open the .env file and add these exact lines to set the database and mail environment variables: DB_CONNECTION=mysql, DB_HOST=127.0.0.1, DB_PORT=3306, DB_DATABASE=laravel_db, DB_USERNAME=root, DB_PASSWORD=secret, MAIL_MAILER=smtp, MAIL_HOST=smtp.mailtrap.io, MAIL_PORT=2525, MAIL_USERNAME=null, MAIL_PASSWORD=null, MAIL_ENCRYPTION=null.
Laravel
Need a hint?

The .env file holds key-value pairs for configuration. Add each variable on its own line.

2
Configure database settings in config/database.php
Open config/database.php and set the 'mysql' connection array to use the environment variables by adding these lines: 'driver' => env('DB_CONNECTION', 'mysql'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', '').
Laravel
Need a hint?

Use the env() helper function to read environment variables with a default fallback.

3
Configure mail settings in config/mail.php
Open config/mail.php and set the mail configuration array to use environment variables by adding these lines: 'mailer' => env('MAIL_MAILER', 'smtp'), 'host' => env('MAIL_HOST', 'smtp.mailtrap.io'), 'port' => env('MAIL_PORT', 2525), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'encryption' => env('MAIL_ENCRYPTION').
Laravel
Need a hint?

Use the env() helper for mail settings just like for database settings.

4
Verify Laravel reads environment variables correctly
In routes/web.php, add a route /env-check that returns a JSON response with keys db_connection and mail_mailer set to env('DB_CONNECTION') and env('MAIL_MAILER') respectively.
Laravel
Need a hint?

Use a closure route and response()->json() to return environment variables for testing.