I want to combine multiple databases in my system. Most of the time the database is MySQL; but it may be different in the future, i.e. administrators can generate reports like this, which is thesource of using heterogeneous database systems.
So my question isDoes Laravel provide any Facadeto handle this situation? Or does any other framework have features that better suit the problem?
In Laravel 5.1, you specify the connection:
$users = DB::connection('foo')->select(...);By default, Laravel uses the default connection. Pretty simple, isn't it?
Read more here:http://laravel.com/docs/5.1/database#Visit the connection
From the Laravel Documentation: You can access each connection when using multiple connections, through the
DBconnection method on the appearance. The name passed to the connection method should correspond to one of the connections listed in theconfig/database.phpconfiguration file:$users = DB::connection('foo')->select(...);Define connection
Use
.env>= 5.0 (or higher)Use
config/database.php'mysql' => [ 'driver' => env('DB_CONNECTION'), 'host' => env('DB_HOST'), 'port' => env('DB_PORT'), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), ], 'pgsql' => [ 'driver' => env('DB_CONNECTION_PGSQL'), 'host' => env('DB_HOST_PGSQL'), 'port' => env('DB_PORT_PGSQL'), 'database' => env('DB_DATABASE_PGSQL'), 'username' => env('DB_USERNAME_PGSQL'), 'password' => env('DB_PASSWORD_PGSQL'), ],No
.envapp/config/database.phpArchitecture/Migration
Run the
connection()method to specify the connection to use.Schema::connection('pgsql')->create('some_table', function($table) { $table->increments('id'): });Alternatively, define a connection at the top.
Query Builder
$users = DB::connection('pgsql')->select(...);model
(In Laravel >= 5.0 (or higher))
Set
in the model$connectionVariablesclass ModelName extends Model { // extend changed protected $connection = 'pgsql'; }eloquent
(In Laravel
Set
in the model$connectionVariablesclass SomeModel extends Eloquent { protected $connection = 'pgsql'; }Trading Mode
DB::transaction(function () { DB::connection('mysql')->table('users')->update(['name' => 'John']); DB::connection('pgsql')->table('orders')->update(['status' => 'shipped']); });or
DB::connection('mysql')->beginTransaction(); try { DB::connection('mysql')->table('users')->update(['name' => 'John']); DB::connection('pgsql')->beginTransaction(); DB::connection('pgsql')->table('orders')->update(['status' => 'shipped']); DB::connection('pgsql')->commit(); DB::connection('mysql')->commit(); } catch (\Exception $e) { DB::connection('mysql')->rollBack(); DB::connection('pgsql')->rollBack(); throw $e; }You can also define the connection at runtime via the
setConnectionmethod or theonstatic method:class SomeController extends BaseController { public function someMethod() { $someModel = new SomeModel; $someModel->setConnection('pgsql'); // non-static method $something = $someModel->find(1); $something = SomeModel::on('pgsql')->find(1); // static method return $something; } }Test version (Updated)