Easily Manage Multiple Database Connections in Laravel

You will soon realize that a Multiple Database Connection in Laravel setup is necessary if your application is expanding swiftly. Perhaps you want a third database for legacy records, a second for reporting, and a third for user data. Laravel allows you to manage several database connections in a neat and organized manner rather than cramming everything into a single database.

You will go through a useful, beginner-friendly instruction on Laravel multiple database connections in this post. You will learn how to set up additional connections, use them in queries and models, and retain the readability of your code.

Why a multi-database setup is useful

You might use Multiple Database Connection In Laravel when you want to:

  • Keep analytics and reporting separate from transactional data
  • Connect to databases owned by external systems or vendors
  • Gradually migrate from an old database to a new one
  • Split read and write operations across different servers
  • Isolate high-traffic features on their own database for performance

A basic example from real life is an online retailer that uses one database for customers and orders and another just for hefty reporting and BI dashboards. Analysts continue to get new data, and the customer-facing website remains responsive.

Configuration of multi-database setup in Laravel

Let’s start with configuring Laravel application with multiple database connection. Before we move forward with code, you need to plan about how many connection do you really need and which connection use which table and model. Lastly, which part of application will use which connection. Let’s start with adding dual database connection.

Add extra database credentials to .env

Your main connection might already exist:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=app_main
DB_USERNAME=root
DB_PASSWORD=

Now add another set for, say, a reporting database:

DB_REPORTING_CONNECTION=mysql
DB_REPORTING_HOST=127.0.0.1
DB_REPORTING_PORT=3306
DB_REPORTING_DATABASE=app_reporting
DB_REPORTING_USERNAME=root
DB_REPORTING_PASSWORD=

For more database connection, you can add as many as needed.

Define multiple connections in config

Next, we need to point out Laravel how to use those environment variables. Open config/database.php and extend the connections array:

return [
    'default' => env('DB_CONNECTION', 'mysql'),

    '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',
        ],

        'mysql_reporting' => [
            'driver' => env('DB_REPORTING_CONNECTION', 'mysql'),
            'host' => env('DB_REPORTING_HOST', '127.0.0.1'),
            'port' => env('DB_REPORTING_PORT', '3306'),
            'database' => env('DB_REPORTING_DATABASE', 'forge'),
            'username' => env('DB_REPORTING_USERNAME', 'forge'),
            'password' => env('DB_REPORTING_PASSWORD', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ],
    ],
];

Here, mysql is your default connection and mysql_reporting is the new one. You can add more connections following the same pattern.

Run migrations on specific connections

When you have multiple connections, you often want separate migration sets. For a reporting database, you can run:

php artisan migrate --database=mysql_reporting

This command tells Laravel exactly where to run those migrations.

Example of multiple database connections in Laravel

Now that the configuration is ready, let’s see how to use multiple database connections in Laravel with simple, real-world style code. Think of a SaaS app with:

  • A main database for users and subscriptions
  • A reporting database for dashboards and scheduled PDF reports

You want user signups to stay fast while heavy reporting queries run elsewhere.

Use a specific connection in queries

You can choose a connection on the fly using the DB facade:

use Illuminate\Support\Facades\DB;

$dailyReports = DB::connection('mysql_reporting')
    ->table('daily_reports')
    ->whereDate('report_date', today())
    ->get();

This example shows a laravel connect to multiple databases example where only reporting data touches mysql_reporting. Your default queries still use the main mysql connection:

$users = DB::table('users')->latest()->limit(10)->get();

Laravel keeps each connection separate behind the scenes.

Use different connections per model

A very common pattern is to laravel use different database connections for models. For instance, you might store reports in a reporting database and users in the main one. The app/Models/User.php might use the default database, so you do not set a $connection property at all.

For a Report model that lives in the reporting database:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Report extends Model
{
    protected $connection = 'mysql_reporting';

    protected $table = 'reports';

    protected $fillable = [
        'user_id',
        'report_date',
        'total_orders',
        'total_revenue',
    ];
}

Now, whenever you work or query with Report model, Laravel automatically uses mysql_reporting connection:

$reports = Report::where('report_date', today())->get();

You can treat it like any other Eloquent model, but it hits a different database.

Conclusion

You have learned how a Multiple Database Connection In Laravel configuration can ensure that your application remains fast, organized, and scale-ready. You have learned how to use .env, then config/database.php, and then how to use queries and models to reference a connection.

Using these techniques, you can connect to legacy databases, maintain a separate reporting database, or manage tenants in a multi-tenant application without making your codebase a mess.