Join two MySQL tables in different databases on the same server with Laravel Eloquent

Join two MySQL tables in different databases on the same server with Laravel Eloquent

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Joining Tables Across Databases in Laravel Eloquent

Laravel's default connection manager handles multiple databases by treating them as distinct entities. When you define separate connections for database1 and database2 in your config/database.php, Eloquent automatically associates each model with its specific configuration. This isolation works perfectly for standard CRUD operations, but it creates a friction point when you need to join tables residing in different schemas on the same MySQL server.

Standard database joins rely on the query engine executing a single SQL statement against one logical connection context. When you attempt ModelA::with('modelB')->get(), Laravel generates separate queries or a join that assumes both tables live within the same database namespace defined by the active connection. Since your models are bound to different connection names (mysql and mysql2), a naive join will fail because Eloquent doesn't know how to bridge the gap between two distinct connection definitions in a single query execution context.

To achieve the desired result, you must bypass the automatic model binding and construct a raw query that explicitly references both connections. This approach requires switching from the high-level Eloquent syntax to DB::table or Schema::connection, ensuring the SQL statement knows exactly which database schema to pull data from for each table involved in the join.

The most direct solution involves using the withConnection method available on query builders, though this is often more useful when you need to split a single model's queries across connections. For a strict join between two tables with different connection names, you typically need to execute the join logic within a raw query block or use the DB facade to specify the connection for each table explicitly.

Consider a scenario where you have a User model in database1 and an Order model in database2. Both share a common id. You cannot simply define a relationship like $user->orders() because Eloquent will try to build the query against the primary connection (mysql). Instead, you must construct the join manually:

$users = DB::connection('mysql')
    ->table('table1')
    ->join(DB::connection('mysql2')->getTable('table2'), 'table1.id', '=', 'table2.id', 'inner')
    ->select('table1.*', 'table2.status as order_status')
    ->get();

In this example, DB::connection('mysql2')->getTable('table2') explicitly tells the query builder to resolve the table name within the context of the secondary database. This ensures the generated SQL looks like:

SELECT * FROM database1.table1
JOIN database2.table2 ON database1.table1.id = database2.table2.id

This pattern is necessary because standard Eloquent relationships assume a single database prefix. By forcing the query builder to handle the join logic, you regain control over the SQL generation and ensure the correct schemas are referenced.

While this works, it sacrifices some of the magic that Eloquent provides, such as automatic model casting or relationship guards. You must manually handle attributes like timestamps or soft deletes if they differ between the two tables. Additionally, performance implications arise when joining across databases; while the physical data resides on the same AWS RDS instance, the logical overhead of managing multiple connection contexts can introduce slight latency compared to a local join.

Another viable approach is to use DB::statement or raw queries if you require complex filtering that doesn't map cleanly to Laravel's query builder methods. However, for most read-heavy operations, the join method demonstrated above offers a clean separation of concerns without requiring you to maintain custom SQL files. Always remember to verify that your user account (user1 and user2) possesses the necessary privileges across both databases to execute cross-database joins, as MySQL permissions are sometimes restrictive regarding mixed-schema access depending on the specific version and configuration.

For more complex scenarios involving multiple joins or aggregations across these disparate schemas, constructing the query string dynamically using the DB facade remains the most robust strategy. It allows you to leverage Laravel's security features like prepared statements while retaining full control over the join logic required to bridge your distributed logical data stores.

Our network