Learn how to manage environment variables in Laravel using the .env file. Master configuration best practices to keep your application secure and portable.
Previously in this course, we walked through installing Laravel and exploring the directory structure. Now that you know where the files live, it’s time to learn how to make your application dynamic.
In professional software development, you never hardcode credentials like database passwords or API keys directly into your source code. If you do, you risk leaking them to version control systems like GitHub. Laravel solves this with the .env file.
The .env file is a plain-text file located in the root of your Laravel project. It acts as a bridge between your server's environment and your application code.
Think of it as a set of "settings" that change depending on where the code is running—like your laptop, a staging server, or a production environment. Because this file contains sensitive credentials, it is listed in your .gitignore file by default. This ensures your secrets never leave your machine.
Laravel doesn't read the .env file directly throughout your application. Instead, it uses a central configuration system located in the config/ directory.
If you open config/database.php, you will see code that looks like this:
PHP'host' => env('DB_HOST', '127.0.0.1'),
The env() helper function looks for a key inside your .env file. If it finds it, it uses that value. If it doesn't, it falls back to the second argument (in this case, 127.0.0.1). This setup allows you to keep your logic clean while keeping your environment-specific data isolated.

To modify an environment variable, simply open your .env file in your code editor. Let's say you want to change your application name. Find the APP_NAME key:
.envAPP_NAME=Laravel
Change it to:
.envAPP_NAME="Task Manager"
Crucial: After making changes to your .env file, you must restart your local development server (php artisan serve) for the changes to take effect. Laravel caches these values during the request lifecycle for performance.
The most important line in your .env file is APP_KEY. This is a random string used to secure your user sessions and encrypted data.
If you ever accidentally expose this key in a public repository, you should treat it as compromised. You can generate a new one at any time by running:
Bashphp artisan key:generate
This command will automatically update your .env file with a fresh, secure string.
.env file.DB_DATABASE key. Change it to task_manager.sqlite.config/database.php is correctly pointing to this environment variable.php artisan key:generate in your terminal to see how the file updates..env.example file in your root folder. This file is a template. Try copying your current .env configuration into it (excluding the actual secrets) so other developers know which variables they need to set up..env file to version control. If you have already done so, remove it immediately and rotate your keys.config/ file using the env() helper. This prevents your app from crashing if a variable is missing.APP_NAME="Task Manager").The .env file is your primary tool for managing environment-specific settings and sensitive credentials. By using the env() helper in your config/ files, you decouple your code from its environment, making your app easier to deploy and keep secure. Always keep your APP_KEY private and use .env.example to document required settings for your team.
Up next: We will dive into The Laravel Application Lifecycle to understand exactly how a request travels from the browser to your code.
Learn how to use the Laravel installer to scaffold a new project and master the framework's directory structure to navigate your application with confidence.
Read moreMaster your Laravel environment setup by installing PHP, Composer, and SQLite. Get your terminal configured to start building our Task Manager application.
The Laravel Application Lifecycle
Initializing the Task Manager Project
Defining Basic Web Routes
Using Route Parameters
Creating Your First Controller
Returning Responses and Redirects
Task Manager: Implementing the Task List Route
Introduction to Blade Templating
Using Blade Layouts and Sections
Implementing Blade Partials
Mastering Blade Directives for Loops and Conditionals
Task Manager: Building the User Interface
Understanding Database Migrations
Working with Eloquent Models
Performing Basic CRUD Operations
Seeding the Database
Task Manager: Displaying Real Database Records
Capturing User Input from Forms
Introduction to Laravel Validation
Customizing Validation Error Messages
Using Form Requests for Validation
Introduction to Authentication
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation