Posts Learn Components Snippets Categories Tags Tools About

How to Use Multiple Databases in Laravel Project

Learn how to set up Laravel with multiple databases environments and get your data across different databases to ensure a more scalable project

Created on Oct 07, 2021


In this short snippet, you will learn how to set up multiple databases for your Laravel Project. The steps are simple and easy to follow so let's get started.

Update Configuration

Laravel provides multiple database configurations and what you need to do is to add the additional configuration aside from the original one that they come by default. Within the "config/database.php" configuration you can create a new connection and for this example, I will be showing the one for "MySQL".
The configuration is as follows do note that depending on your Laravel and MySQL versions you might need some other configurations. Do note that you can refer to the Laravel Database Configuration in GitHub for the latest one.

return [
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', ''),
        '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' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],

    'mysql2' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL2'),
        'host' => env('DB_HOST2', ''),
        'port' => env('DB_PORT2', '3306'),
        'database' => env('DB_DATABASE2', 'forge'),
        'username' => env('DB_USERNAME2', 'forge'),
        'password' => env('DB_PASSWORD2', ''),
        'unix_socket' => env('DB_SOCKET2', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA2'),
        ]) : [],

Update Environment Variable

You also need to update the environment variable located in ".env" file in your root project. Do note that your environment variable must be the same as the one you specified in the "config/database.php" file.


Defining Schema

When defining table schema then you can specify the "connection" like below. Running the migrate column will create a table inside "mysql2" database.

    ->create('posts', function($table) {

Query Builder

When querying up the database, you can also specify the "connection" on to which the database to query.

$users = DB::connection('mysql2')

Eloquent Model

For the eloquent model, you can specify the connection directly from the model itself and it's as follows.

class Post extends Eloquent {
    protected $connection = 'mysql2';

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️