10

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?

3 Answers 3

7

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm assuming that the database is set for each user. If not, sessions are not the way to go.
furthermore to what @J.T. Grimes commented earlier, if the database is used for sessions (or users etc.) you'll be having a hard time to get things started in order or in case of sessions not at all because you can't tell the database any longer without sessions but you don't have the sessions until you can tell the database. Rule of thumb: Keep your apps configuration out of the database for your apps database. You can configure the default database in Laravel.
5
Answer recommended by PHP Collective

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';

If not make the default models use the default connection, then you can run your configured app by default. In test you use the test configuration (e.g. using the test database).

1 Comment

This is the correct answer without a doubt.
0

Just like J.T. Grimes said, Config::set() is only set for one request.

I solved this issue by creating a middleware that check if some condition is true and then calls the set() method. This middleware is called in every request, making it some sort of 'global'.

// check if the app is running in test mode
if (config('app.env') == 'test') {
    Config::set('database.default', 'mysql_test');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.