Last active
February 3, 2016 00:53
-
-
Save jdubwelch/5b16f547b8a5d4a5cc01 to your computer and use it in GitHub Desktop.
Multiple Database on Different Servers Laravel Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I define a connection in /app/config/database.php for each database server then.