1

I'm trying to make a laravel use using multiple databases:

  • Laravel version: 5.5.28
  • Php Version 7.2.0
  • Database Driver & version: MariaDB 10.1.29

Define Connections:

return array(

    'default' => 'mysql',

    'connections' => array(

        # Primary/Default database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'bdd1',
            'username'  => 'root',
            'password'  => ''
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'bdd2',
            'username'  => 'root',
            'password'  => ''
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

Define model:

class Products extends Model {

    protected $connection = 'mysql';

}

Define controller:

class ProductController extends BaseController {

    public function find()
    {
        $productModel= new Products;

        $productModel->setConnection('mysql2');

        $product= $productModel->find(1);

        return $product;
    }

}

The code above works without error, but if I change the name of the connection in the controller it continues using what was configured in the .env file

Please, could anyone help me solve this problem?

airton
  • 11
  • 1

2 Answers2

2

You can config: In .env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=bdd1
DB_USERNAME=root
DB_PASSWORD=

DB_CONNECTION_SECOND=mysql2
DB_HOST_SECOND=localhost
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=bdd2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=

In 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'),
],

'mysql2' => [
    'driver' => env('DB_CONNECTION_SECOND'),
    'host' => env('DB_HOST_SECOND'),
    'port' => env('DB_PORT_SECOND'),
    'database' => env('DB_DATABASE_SECOND'),
    'username' => env('DB_USERNAME_SECOND'),
    'password' => env('DB_PASSWORD_SECOND'),
],

Good luck !

Joan Nguyen
  • 372
  • 1
  • 5
0

There are two methods to achieve this, once you change the connection on the fly you have to propagate the connection which you can do like this.

if ($this->propogateConnection) {
    $instance->setConnection($this->getConnectionName());
 }

Another thing is you can use Model::on('connection_name',true) and true is for propagating the connection.

Hope this helps.

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
  • Hello, thanks for the reply, but it is still not clear to me how to correct this problem ... could you show me an example, video or site where this solution exists? – airton Jan 08 '18 at 03:25