In my database.php
, I have TWO databases configured.
\'db1\' => array(
\'driver\' => \'pgsql\',
\'host\' => \'localhost\',
Config::set
is only going to work on a per-request basis, so you're probably going to want to set your database in the Session and grab it on subsequent requests.
You have some options on where to do that. /app/start/global
would be one option. In the controller constructor would be another. You could register a service provider to do it, too.
Below is an example of what the code might look like [warning: untested code!] in the controller constructor:
public function __construct() {
if(Session::has('selected_database'){
Config::set('database.default',Session::get('selected_database'));
} else {
return Redirect::to('database_choosing_page');
}
}
and an update to your database setting function:
public function postChangeDb() {
$db = Input::get('db');
Session::put('selected_database',$db);
Config::set('database.default', $db);
}
Have you tried simply to change the default connection in app/config/database.php
?
'default' => 'db2'
If it's not the case then please provide more information on the problem.
Edit: So it seems you have all connections hardcoded in the models. Try updating the models like that:
protected $connection = 'db2';