Skip to content

Instantly share code, notes, and snippets.

@jdubwelch
Last active February 3, 2016 00:53
Show Gist options
  • Save jdubwelch/5b16f547b8a5d4a5cc01 to your computer and use it in GitHub Desktop.
Save jdubwelch/5b16f547b8a5d4a5cc01 to your computer and use it in GitHub Desktop.
Multiple Database on Different Servers Laravel Solution
<?php
use Eloquent;
class BaseModel extends Eloquent {
/**
* The default connection
*/
protected $connection = 'db1-mysql';
/**
* The name of the database to add chunk to
*/
protected $chunkedDatabase;
function __construct(array $attributes = array())
{
// If the model uses a chunked database
if ($this->chunkedDatabase)
{
// Set the chunked database name
// client.chunk is defined in a filter (i would like a better way to do this though)
$database = $this->chunkedDatabase . app('client.chunk');
// Update the model's table to include the database in the name
$this->table = $database . '.' . $this->table;
// Get the connection of the chunked data from our servermap model
$this->connection = app('Servermap')->getConnection($database);
}
}
}
<?php
class Page extends BaseModel {
/**
* Database is chunked... client1, client2, client3, etc
*/
protected $chunkedDatabase = 'client';
/**
* The table associated with the model.
*/
public $table = 'pages';
// Rest of the standard model stuff below
}
@jdubwelch
Copy link
Author

I define a connection in /app/config/database.php for each database server then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment