php - Two Databases in Eloquent ORM without Laravel -
i use eloquent orm without laravel, works ok 1 database, don't know how use second database.. use capsule configure eloquent
database.php file:
require 'vendor/autoload.php'; use illuminate\database\capsule\manager capsule; $capsule = new capsule; $capsule->addconnection( array( 'driver' => 'pgsql', 'host' => 'localhost', 'database' => 'database01', 'username' => 'postgres', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '' ) ); $capsule->booteloquent();
how can add second database? (i see different configuration of database.php file starts "return array(..." don't know how use capsule or other way)
thanks!
i facing same problem , took me while find answer in forum.
this ended doing
<?php use illuminate\database\capsule\manager capsule; //connection first database $capsule = new capsule; $capsule->addconnection([ 'driver' => $app->config->get('db.driver'), 'host' => $app->config->get('db.host'), 'database' => $app->config->get('db.name'), 'username' => $app->config->get('db.username'), 'password' => $app->config->get('db.password'), 'charset' => $app->config->get('db.charset'), 'collation' => $app->config->get('db.collation'), 'prefix' => $app->config->get('db.prefix'), ], 'default'); //the important line // connection second database $capsule->addconnection([ 'driver' => $app->config->get('db2.driver'), 'host' => $app->config->get('db2.host'), 'database' => $app->config->get('db2.name'), 'username' => $app->config->get('db2.username'), 'password' => $app->config->get('db2.password'), 'charset' => $app->config->get('db2.charset'), 'collation' => $app->config->get('db2.collation'), 'prefix' => $app->config->get('db2.prefix'), ], 'secondary_db'); // important line $capsule->setasglobal(); $capsule->booteloquent();
don't think kind of syntax: $app->config->get('db2.driver'). i'm calling parameters file, that's all.
what should see "the important line" added comment
then, can go ahead , use on secondary models let eloquent know want use secondary database
class domains extends eloquent { protected $connection = 'secondary_db'; . . .
you don't need specify connection variable in models using default database.
Comments
Post a Comment