In my database.php, I have TWO databases configured.
'db1' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'db1',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'db2' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'db2',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
So by default db1 is set as default DB initially. Now I want to switch the default database to 'db2' by selecting an option from 'select' dropdown. This will do a post AJAX request to the controller method in which I do
public function postChangeDb() {
$db = Input::get('db');
Config::set('database.default', $db);
}
Once this is done, I 'refresh' the page, but the connection is still at 'db1'.
I also tried the following
public function getTest() {
Config::set('database.default', 'db1');
$users = User::all();
echo sizeof($users); // returns 20
Config::set(database.default', 'db2');
$users = User::all();
echo sizeof($users); // returns 50 - which is correct!
}
And the above works fine and it successfully switches the database. Is the switch 'per request' basis?