Laravel Multi Database Connection Join table

php_dvp picture php_dvp · Nov 28, 2016 · Viewed 7.3k times · Source

Datebase 1 = db1 Database 2 = db2

I have two databases connection, I wanted to run a eloquent or DB which joining connection1 and connection 2 table(db1.users and db2.users), and check connection1 id = connection 2 or not.

can someone guide me how to do that?

Appreciate that if someone could guide me what to do.

db1 user id name ori_id

db2 ori_user id name

select db2.ori_user.name from db1.user join db1.user on db1.user.ori_id = db2.ori_user.id

Answer

The Alpha picture The Alpha · Nov 28, 2016

Yes, it's possible as long as they are on the same server, for example:

$result = \DB::table('db1.users')
    ->join('db2.users', 'db2.users.id', '=', 'db1.users.id')
    ->select('db1.users.id as db1_id', 'db1.users.email as db1_email', 'db2.users.*')
    ->get();

Make sure the user has access/privilege to use both databases.