0

I have use multiple database in my project. But is that possible to use multiple connection in single query in laravel? This is my connection config.

 '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,
    ],

I want to join a tabel in my mysql connection to mysql_pdd table. Is that possible?

1 Answer 1

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');
}

Using $mysql & $mysql_pdd you can perform joins as well by code not by query.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.