Laravel Multiple DB Connections in Laravel Using Eloquent


Example

There are multiple ways to define which connection to use in the Eloquent models. One way is to set the $connection variable in the model:

<?php

class SomeModel extends Eloquent {

    protected $connection = 'mysql2';

}

The connection can also be defined at runtime via the setConnection method.

<?php

class SomeController extends BaseController {

    public function someMethod()
    {
        $someModel = new SomeModel;

        $someModel->setConnection('mysql2');

        $something = $someModel->find(1);

        return $something;
    }
}