How to use multiple databases in Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Utilizing Multiple Databases in Laravel Applications
Introduction
There are various use cases where a single database is insufficient to handle all the diverse tasks required by your application, necessitating the need for multiple databases. In such scenarios, Laravel provides a robust solution that allows you to easily manage and interact with different databases from within your application. This blog post will guide you through the process of leveraging this functionality while highlighting best practices and tips.
Understanding Multiple Database Support in Laravel
Laravel's database system is highly flexible and adaptive, allowing developers to work with multiple databases simultaneously. It achieves this by providing a universal API called Eloquent which abstracts the underlying storage layer, enabling seamless data manipulation across various databases. Additionally, it offers a dedicated facade named \Illuminate\Support\Facades\DB for interacting with databases directly through query builders.
Configuring Multiple Databases in Laravel
Before you proceed, ensure that you have installed the intended databases on your system and are familiar with their respective configurations and connection details. In order to use multiple databases within Laravel, perform the following steps:
1. Create separate database configuration files.
For each database, create a unique configuration file under the `config/database.php` directory. These configuration files should be named based on your preference (e.g., config/database_1.php, config/database_2.php, etc.). Within each configuration file, specify the appropriate connection details such as the host, username, password, database name, etc.
2. Configure Laravel's global configuration file.
This is where you'll define which databases are available for your application to connect to. Locate the `config/database.php` file and add the newly created configuration files under the 'connections' array:
```php
return [
// ... other connections
'connections' => [
// Custom database connection names
'my_db_1' => [
'driver' => env('DB_CONNECTION_1', 'mysql'),
'host' => env('DB_HOST_1', 'localhost'),
'port' => env('DB_PORT_1', null),
'database' => env('DB_DATABASE_1', 'forge'),
'username' => env('DB_USERNAME_1', 'root'),
'password' => env('DB_PASSWORD_1', null),
],
// Custom connection 2, and so on
]
];
```
3. Define aliases for your database connections.
Create a new file named `aliases.php` under the project root directory (the same location as config/app.php) and add the following lines to define your custom database connection names:
```php
return [
// Custom database connection names
'DB_1' => env('DB_CONNECTION_1', 'mysql'),
'DB_2' => env('DB_CONNECTION_2', 'pgsql'),
];
```
Interacting with Multiple Databases in Laravel
Now, you're ready to work with multiple databases within your application. Follow these steps:
1. Use Eloquent Models.
Eloquent models abstract the underlying storage layer, making it convenient for developers to interact with data without worrying about the specific database being used. In order to use a model with a different database connection, simply define its connection as an argument in the constructor:
```php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $connection = 'DB_2';
// Your model logic goes here...
}
```
2. Utilize Facades for Direct Database Interaction.
Laravel's database facade, \Illuminate\Support\Facades\DB, provides a convenient way to perform queries and interact directly with databases through query builders. To work with a specific database connection, pass the desired connection name as an argument:
```php
use Illuminate\Support\Facades\DB;
$products = DB::connection('DB_3')->table('products')->get();
```
Conclusion
By following these guidelines and best practices, you can effectively manage multiple databases within your Laravel application. The ease with which you can work with different database systems allows for increased flexibility and scalability in your projects. Remember to incorporate proper database management strategies, such as regular backups and efficient data partitioning, in order to enhance the overall performance and security of your system. With the assistance of Laravel's multiple database support, you can confidently tackle diverse application requirements.