1

I am not sure if this is even possible but i have this scenario with my Laravel install.

I am running 3 live databases (will be more over time), I have one laravel installation, and what I want to do is merge all the data using the different connections and run 1 query to get all the data with all relationships etc.... basically want to use eloquent. All the database are the same in terms of table designs, index, keys etc, but I am not sure if its even possible!

I want to be able to write one query for example: Users::all(); which uses all the connections at once.

The only way in which i know it will work would be doing is this way.

'connection1' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
'connection2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

DB::connection('connection1')->table('table_here')->get();

Anyone know if this is even possible.

Hayden Sweet
  • 170
  • 1
  • 16
  • check this : [http://fideloper.com/laravel-multiple-database-connections](http://fideloper.com/laravel-multiple-database-connections) – Meera Tank Apr 10 '17 at 07:22
  • I am using the different connections at the moment and making queries based on that, but i want to be able to write one query for example: `Users::all();` which uses all the connections at once. – Hayden Sweet Apr 10 '17 at 07:28

1 Answers1

2

Inside of your datbase configuration file - likely app/config/database.php - you can define more than one database connection of any type. In fact, you can define as many connections as you'd like. For instance, if your application has to pull data from 2 MySQL databases, you can define them both separately:

see this link: http://fideloper.com/laravel-multiple-database-connections