Skip to content

Instantly share code, notes, and snippets.

@sineld sineld/laravel.php
Created Aug 28, 2012

Embed
What would you like to do?
Multiple Databases
// http://forums.laravel.com/viewtopic.php?pid=6386#p6386
You can specify different connections in application/config/database.php. The keys for the connections array can be anything, they do not need to be "sqlite", "mysql", etc.
// application/config/database.php
'default' => 'fred',
'connections' => array(
'fred' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
),
'wilma' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
),
'barney' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database3',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
),
),
Then, if you're using fluent queries...
$user = DB::connection('wilma')->table('users')->where('id', '=', 1)->first();
Or, if you're using eloquent, you can specify the connection on the model...
class Dino extends Eloquent {
public static $connection = 'barney';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.