Environment Configuration and Database Setup
Learn how to use the .env file and connect Laravel to a MySQL database securely.
Laravel uses a .env file to store environment-specific settings like database credentials. ## Why .env is important - Keeps secrets out of code - Easy to change per environment - Makes deployment safer ## Configure database Open the .env file and update: ```env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD= ``` Create the database manually in MySQL. ## Test connection ```bash php artisan migrate ``` If it runs without errors, your database is connected. ## How config flows ```mermaid flowchart LR A[.env] --> B[config/database.php] B --> C[Laravel DB Manager] C --> D[(MySQL)] ``` In the next tutorial, we will create tables using migrations instead of writing raw SQL.