i use Eloquent ORM without Laravel, it works OK with one database, but i don't know how use a second database.. i use Capsule to configure eloquent
DATABASE.PHP FILE:
require 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as 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 i add a second database? (i see a different configuration of database.php file that starts with a "return array(..." but i don't know how use that for Capsule or other way)
thanks!
I was facing the same problem and it took me a while to find the answer in another forum.
this is what I ended up doing
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
//connection to 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 to 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'); // the important line
$capsule->setAsGlobal();
$capsule->bootEloquent();
Don't think too much about this kind of syntax: $app->config->get('db2.driver'). I'm just calling my parameters from another file, that's all.
What you should do is see "the important line" to which I added a comment
then, you can go ahead and use this on your secondary models to let Eloquent know that you actually want to use the secondary database
class Domains extends Eloquent
{
protected $connection = 'secondary_db';
.
.
.
you don't need to specify the connection variable in the models using the default database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.