Laravel-在单个查询中使用多重连接

时间:2018-09-03 03:23:04

标签: laravel

我在项目中使用了多个数据库。但是有可能在laravel中的单个查询中使用多个连接吗?这是我的连接配置。

 'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

    'mysql_pdd' => [
        'driver' => 'mysql',
        'host' => env('DB_PDD_HOST', '127.0.0.1'),
        'port' => env('DB_PDD_PORT', '3306'),
        'database' => env('DB_PDD_DATABASE', 'forge'),
        'username' => env('DB_PDD_USERNAME', 'forge'),
        'password' => env('DB_PDD_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

我想在与mysql表的mysql_pdd连接中加入表格。有可能吗?

1 个答案:

答案 0 :(得分:1)

Yes, you can but for that you need to put condition, when you want to connect 'mysql' and when 'msql_pdd'.

if (mysql_connection){
 $mysql =DB:: reconnect('mysql');
}else if(mysql_pdd_connection){
 $mysql_pdd =DB:: reconnect('mysql_pdd');
}

使用$ mysql和$ mysql_pdd,您还可以通过代码而不是通过查询执行联接。