Skip to content

Instantly share code, notes, and snippets.

@sineld
Created August 28, 2012 05:18
Show Gist options
  • Save sineld/3495179 to your computer and use it in GitHub Desktop.
Save sineld/3495179 to your computer and use it in GitHub Desktop.
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