Skip to content
Advertisement

How to join two tables in mysql that has different connections of mysql on different server laravel

I tried this query below and got a database query builder not converted to string error

ModelA::join(DB::connection('mysql2')->table('table1'), 'modela.id','table1.id')->get();

Advertisement

Answer

Eloquent does not support joining data from different connections.

If both databases are stored on different servers, the only thing you can do is to fetch relevant data from both connections and then join it programatically.

Joining data from different databases is possible if both databases are stored on the same server. In order to do that, you’ll need to prefix table names with database names. With Eloquent, you’d additionally need to use the same connection to access both databases.

Then, it could look like this:

ModelA::join('database2.table1', 'modela.id', 'database2.table1.id')->get()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement