Set database for model?

eloquent, laravel, laravel-4

Solution

// Model
class Client extends Eloquent
{
    protected $connection = 'masterDb';
}


// config/database.php
'masterDb' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'name',
    'username'  => 'user',
    'password'  => 'pass',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

You can create as many named connections as you wish. Set one of them as default, each model can use any of these connections later.

Problem

I'm working on a Laravel 4 based site which works over multiple databases. There is one query I need to run on each request that pulls a record from a different database. Is there a way I can somehow tie this particular model to the other database so I can just retrieve it as usual? ``` $client = Client::find(Session::get('client_id')); ``` Any advice appreciated. Thanks

Original source