Question

I have a project that connects with different database, and i want to join those tables from different database using laravel 4. I searched already about this issue but i can't find the right answer. Is it possible to join tables from different database in laravel 4? If possible,does anyone can help me how to build a query for that?

Was it helpful?

Solution

You can do exactly that using the DB class:

$results = DB::select('select * from database1.users u1 LEFT JOIN database2.users u2 ON u1.id = u2.id WHERE u2.id = ?', array(5));

You can also use Fluent to build the query, which would look something like:

$users = DB::table('db1.users as db1')
  ->select('db1.*')
  ->leftJoin('db2.users as db2', 'db1.id', '=', 'db2.id')
  ->where('db2.id', 5)
  ->get();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top