Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用2个数据库连接进行Laravel查询_Php_Mysql_Database_Laravel_Eloquent - Fatal编程技术网

Php 使用2个数据库连接进行Laravel查询

Php 使用2个数据库连接进行Laravel查询,php,mysql,database,laravel,eloquent,Php,Mysql,Database,Laravel,Eloquent,我有一个可以在phpmyadmin中完美执行的查询: SELECT f.`id`, f.`cod_factura`,f.`empresa` FROM `gen`.`facturas` f WHERE f.`id_cliente` IN(SELECT `id` FROM `gen`.`clientes` WHERE `dni` IN(SELECT `dni` FROM `ciiis17`.`users` WHERE `id` IN(SELECT `user_id` FROM `ciiis1

我有一个可以在phpmyadmin中完美执行的查询:

SELECT f.`id`, f.`cod_factura`,f.`empresa` FROM 
`gen`.`facturas` f WHERE f.`id_cliente` IN(SELECT 
`id` FROM `gen`.`clientes` WHERE `dni` IN(SELECT 
`dni` FROM `ciiis17`.`users` WHERE `id` IN(SELECT `user_id` FROM 
`ciiis17`.`registrations` WHERE `has_bill`=1))) AND `enviado`=0  
AND `concepto`='1' 
现在我试着用拉威尔的话来回答这个问题。我正在尝试这样的事情:

$result = DB::connection('gen')
              ->table('facturas')
              ->select(DB::raw('id, cod_factura, empresa'))
              ->whereIn('id_cliente', DB::table('clientes')
                                      ->select(DB::raw('id'))
                                      ->whereIn('dni', DB::connection('ciiis17')
                                                       ->table('users')
                                                       ->select(DB::raw('dni'))
                                                       ->whereIn('id', DB::table('registrations')
                                                                       ->select(DB::raw('user_id'))
                                                                       ->where(['has_bill', 1],['enviado', 0],['concepto', '1']))))->get();
但我得到了这个错误:

“Connection.php第729行中的QueryException: SQLSTATE[42S02]:未找到基表或视图:1146表“gen.registrations”不存在“

它要求“gen.registrations”,但应该是“ciiis17.registrations”

最后执行的SQL是:

"select id, cod_factura, empresa from `facturas` where `id_cliente` in 
(select id from `clientes` where `dni` in (select dni from `users` where 
`id` in (select user_id from `registrations` where (`0` = ? and `1` = ?))))"

谢谢

config/database.php文件中的连接名是否设置为gen?同时显示迁移,我可以看到laravel在gen连接上查找注册表,这意味着ur ciiis17连接数据库未正确设置!是的,它是这样配置的。我在config/database.php中有连接“gen”和连接“ciiis17”,您可以检查最终查询的外观吗?我认为您无法从两个数据库中进行单个查询。尝试将它们分开。最后的查询如下所示:“从
facturas
where
id\u cliente
in中选择id(从
clientes
where
dni
in(从
users
where
id
in中选择用户id)(从
registrations
where中选择用户id(
0
=?和
1
=?))