Laravel is a powerful PHP framework that simplifies web development with its robust features. One of these features is the ability to connect to multiple databases within a single application. Whether you need to separate data by environment, manage different types of databases, or scale your application, Laravel makes it easy to handle multiple database connections.
In this article, we’ll guide you through the process of setting up and using multiple databases in Laravel. By the end of this tutorial, you’ll be able to configure, manage, and query data from different databases seamlessly.
Why Use Multiple Databases?
Using multiple databases in Laravel can be beneficial for several reasons:
- Data Segmentation: Separate data for different parts of your application, such as user data and analytics.
- Scalability: Distribute data across multiple databases to handle larger loads.
- Security: Isolate sensitive data by storing it in a separate database.
Step 1: Configure Multiple Database Connections
The first step is to define multiple database connections in Laravel’s configuration file.
Example: Setting Up Database Connections
-
Open the
config/database.php
File:In your Laravel project, navigate to the
config/database.php
file. Here, you’ll define your database connections. -
Add Additional Connections:
You’ll find the
connections
array in thedatabase.php
file. This is where you can add multiple database connections:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
'connections' => [ 'mysql' => [ 'driver' => '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', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], 'pgsql' => [ 'driver' => 'pgsql', 'host' => env('DB_PGSQL_HOST', '127.0.0.1'), 'port' => env('DB_PGSQL_PORT', '5432'), 'database' => env('DB_PGSQL_DATABASE', 'forge'), 'username' => env('DB_PGSQL_USERNAME', 'forge'), 'password' => env('DB_PGSQL_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', 'sslmode' => 'prefer', ], ],
In this example, we’ve added a PostgreSQL (
pgsql
) connection in addition to the default MySQL connection. -
Set Up Environment Variables:
Ensure that you have corresponding environment variables in your
.env
file:1 2 3 4 5 6 7 8 9 10 11 12
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_mysql_db DB_USERNAME=your_mysql_user DB_PASSWORD=your_mysql_password DB_PGSQL_HOST=127.0.0.1 DB_PGSQL_PORT=5432 DB_PGSQL_DATABASE=your_pgsql_db DB_PGSQL_USERNAME=your_pgsql_user DB_PGSQL_PASSWORD=your_pgsql_password
Step 2: Querying Data from Multiple Databases
Once your connections are set up, you can easily switch between databases in your queries.
Example: Using Multiple Database Connections in Queries
-
Querying the Default Database:
By default, Laravel uses the connection defined by
DB_CONNECTION
in your.env
file:1
$users = DB::table('users')->get();
-
Querying a Specific Database:
To query a different database connection, you can use the
connection
method:1
$pgsqlUsers = DB::connection('pgsql')->table('users')->get();
This code queries the
users
table from the PostgreSQL database defined in thepgsql
connection. -
Using Multiple Connections in the Same Query:
You can also work with multiple connections within the same query:
1 2
$mysqlUsers = DB::connection('mysql')->table('users')->get(); $pgsqlUsers = DB::connection('pgsql')->table('users')->get();
This is useful when you need to combine data from different databases.
Step 3: Managing Migrations for Multiple Databases
When working with multiple databases, you may need to manage migrations separately for each database.
Example: Running Migrations on Specific Connections
You can specify the database connection when running migrations:
|
|
This command runs the migrations on the PostgreSQL database connection.
Step 4: Using Eloquent with Multiple Databases
Eloquent models can also be configured to use different database connections.
Example: Setting the Connection for an Eloquent Model
In your Eloquent model, you can specify which database connection it should use:
|
|
This makes the User
model interact with the PostgreSQL database by default.
Conclusion
Using multiple databases in Laravel is a powerful feature that provides flexibility and scalability for your application. By following this guide, you can easily configure, manage, and query data from multiple databases in Laravel, allowing you to better structure your application’s data.
Whether you’re separating data by environment, improving security, or scaling your application, Laravel’s support for multiple databases makes it easy to implement.