Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Join queries between multiple Databases that are on different server with Laravel Eloquent?

This is how we handle multiple DB Connections with Laravel, which is a PHP Framework not Python (For whom thinks that this is a duplicate post)

<?php
return array(

    'default' => 'mysql',

    'connections' => array(

        # Our primary database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'host1',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Our secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'host2',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

And this is how you connect to this database.

$user1 = User::on('mysql1')->where(/* ... */)->get()
$user2 = User::on('mysql2')->where(/* ... */)->get()

These are only SELECT queries. Therefore Eloquent works flawlessly.

However, when I want to execute a JOIN query operation between these 2 databases, this seems not possible.

like image 629
SNaRe Avatar asked May 15 '15 06:05

SNaRe


1 Answers

If these 2 databases are on different servers, you can't do this. At least not simply. I read about a storage engine. But I have never tried to do this before.

BUT

If there databases are on the same server, you can do it with a simple join, IF the user has the right permission!

like image 79
Zoltán Fekete Avatar answered Oct 05 '22 08:10

Zoltán Fekete