我有一个连接不同数据库的项目,我想使用 laravel 4 连接来自不同数据库的那些表。我已经搜索过这个问题,但我找不到正确的答案。是否可以在 laravel 4 中连接来自不同数据库的表?如果可能的话,有没有人可以帮助我如何为此建立查询?
最佳答案
您可以使用 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));
您还可以使用 Fluent构建查询,它看起来像:
$users = DB::table('db1.users as db1')
->select('db1.*')
->leftJoin('db2.users as db2', 'db1.id', '=', 'db2.id')
->where('db2.id', 5)
->get();
关于php - 拉维尔 4 : Join tables from different database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519170/