adnan.shabbir's avatar

Remote database connection in joins not working

Hey folks,

I have two databases. one is local , and the other is on the remote server

I have set the connections in env file and database config file

I'm able to fetch data, update data and store data to remote database

but when i use joins, it just stop working by saying that "unknown database forge.optouts"

Basically, it is not picking the remote database connection with joins.

Any help will be highly appreciated.

Best, Adnan Shabbir.

0 likes
5 replies
LaryAI's avatar
Level 58

It's possible that the join query is not specifying the correct database connection. Try explicitly specifying the connection in the join query using the connection method. For example:

DB::connection('remote')->table('table1')
    ->join('remote_db.table2', 'table1.id', '=', 'table2.id')
    ->select('table1.*', 'table2.column')
    ->get();

Make sure to replace remote with the name of the remote database connection in your config/database.php file.

adnan.shabbir's avatar

@LaryAI No, it's not working, it considerss both tables in the same database. In my situation, one table is in remote database and other table is in my local database.

( i know AI will not reply, i just replied it for other members info)

robj's avatar

Do you mean joining a local table to a remote table? Where DB egines are they?

adnan.shabbir's avatar

Yes, joining a local database table with remote database table.

Please or to participate in this conversation.