Laravel Database Configuration
Table of Contents
|
|
1. Introduction to Database Configuration in Laravel
Working with a database is one of the most important parts of building any Laravel application. Whether you are creating a small blog, a business website, or a large-scale enterprise system, you need to configure your database correctly. Laravel makes database management easy and flexible with a simple configuration process, environment variables, and powerful tools like migrations and Eloquent ORM.
In this tutorial, we’ll cover Laravel database configuration from zero to advanced level, including .env setup, multiple database connections, switching drivers, and best practices.
2. Laravel Database Configuration Files
Laravel uses two important files for database setup:
.env File
-
Located in the root of your project.
-
Stores your database credentials.
-
Example:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=
config/database.php
-
Contains database connection settings for different drivers.
-
Example (MySQL section):
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
👉 You don’t need to edit this file often. Instead, you configure everything in .env.
3. Supported Database Drivers in Laravel
Laravel supports multiple database systems:
-
MySQL (most popular, widely used for Laravel projects)
-
PostgreSQL (advanced, enterprise-grade features)
-
SQLite (lightweight, file-based, great for testing)
-
SQL Server (Microsoft’s database engine)
You can switch easily by updating the DB_CONNECTION in your .env file:
4. Step-by-Step: Laravel Database Setup with MySQL
Since MySQL is the most common choice for Laravel, let’s configure it step by step.
Step 1: Create a Database
-
Open phpMyAdmin or MySQL CLI.
-
Create a new database:
Step 2: Update the .env File
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=your_password
Step 3: Test Database Connection
Run:
If the migration works, your database connection is correct.
5. Advanced Database Configuration in Laravel
5.1 Multiple Database Connections
You can connect Laravel to more than one database.
In .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=main_db
DB_USERNAME=root
DB_PASSWORD=secret
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=logs_db
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=secret
In config/database.php:
Now you can query from two databases:
5.2 Using SQLite in Laravel
If you want a quick and lightweight database:
-
Create
database/database.sqlitefile. -
Update
.env:DB_CONNECTION=sqlite DB_DATABASE=/absolute/path/to/database/database.sqlite DB_FOREIGN_KEYS=true
5.3 Database URL Configuration
Laravel also supports a single DATABASE_URL format.
In .env:
In config/database.php:
5.4 Read & Write Connections
For large applications, you may want separate databases for read and write operations.
'mysql' => [
'driver' => 'mysql',
'host' => [
'read' => env('DB_HOST_READ', '127.0.0.1'),
'write' => env('DB_HOST_WRITE', '127.0.0.1'),
],
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
],
6. Testing Database Connection in Laravel
Laravel provides a simple way to test connections:
7. Best Practices for Laravel Database Configuration
-
Never commit
.envto GitHub – it contains sensitive credentials. -
Use environment variables instead of hardcoding in
database.php. -
Enable strict mode in MySQL for better data integrity.
-
Use migrations instead of manually creating tables.
-
Separate read & write connections for performance in large apps.
-
Test locally with SQLite for speed, deploy with MySQL/PostgreSQL in production.
8. Conclusion & Next Steps
In this lesson, you learned how to configure databases in Laravel from scratch. Starting with the .env file and config/database.php, we explored MySQL setup, multiple connections, SQLite, URL configuration, and advanced read/write databases.
With the database configured, you are now ready to move forward with migrations and Eloquent ORM, which make working with databases even easier.