See my reply on this thread http://laravel.io/forum/03-02-2015-how-to-change-default-database-connection-in-controller?page=1#reply-21873
thanks for the solution @astroanu I was still hoping for someone to explain how the model connection gets resolved at runtime though.
Hi. You can simply extend the used base Model class with a new one, and have the following inside it:
/**
* @return string
*/
public function getConnectionName()
{
$databaseConnection = config($this->getConfigFileName().'.database_connection');
if (!empty($databaseConnection)) {
$this->connection = $databaseConnection;
}
return parent::getConnectionName();
}
Then you simply produce a function called 'getConfigFileName()'. This way you specify connection name for any implementations of this new Model extension class dynamically using config file(s).
Hope this helps.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community