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

Time:08-11

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();

CodePudding user response:

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()
  • Related