one server multiple databases

How to Use Multiple Database Connections in Laravel?

In Laravel we can easily use multiple connections to use multiple databases in one Larvel app. Lets implement that. First of all, we need to place Database connections information in our .evn file.

DB_CONNECTION=mysql1
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=secret

DB_CONNECTION=mysql2
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE2=database2
DB_USERNAME2=root
DB_PASSWORD2=secret

Now in our config/database.php file we have an array of multiple connections. By default, it has sqlite, mysql, pgsql and sqlsrv but you can add as many connections as you want. So we will add two connections there.

        'mysql1' => [
            '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' => '',
            '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_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE2', 'forge'),
            'username' => env('DB_USERNAME2', 'forge'),
            'password' => env('DB_PASSWORD2', ''),
            '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'),
            ]) : [],
        ],

Now in migrations we can use any connection we want.

Schema::connection('mysql2')->create('products', function($table)
{
    $table->increments('id'):
});

we can change our queries like this.

$users = DB::connection('mysql2')->select(...);

In our eloquent model we can set connection for any model like this.

class SomeModel extends Eloquent {

    protected $connection = 'mysql2';

}

even in our controller we can do this to switch between connections.

class SomeController extends BaseController {

    public function someMethod()
    {
        $someModel = new SomeModel;

        $someModel->setConnection('mysql2'); // non-static method

        $something = $someModel->find(1);

        $something = SomeModel::on('mysql2')->find(1); // static method

        return $something;
    }

}

I hope now you can easily use multiple databases in one Laravel app. If you have any questions please write us in comments sections. We really appreciate that.