Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set database for model?

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

like image 452
Dan Avatar asked Jul 22 '13 10:07

Dan


1 Answers

// 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.

like image 172
Andreyco Avatar answered Nov 18 '22 03:11

Andreyco