title: Laravel - change the default database connection globally
body: <p>In my <code>database.php</code>, I have TWO databases configured.</p>
  
  <pre><code>'db1' =&gt; array(
    'driver'   =&gt; 'pgsql',
    'host'     =&gt; 'localhost',
    'database' =&gt; 'db1',
    'username' =&gt; 'root',
    'password' =&gt; 'password',
    'charset'  =&gt; 'utf8',
    'prefix'   =&gt; '',
    'schema'   =&gt; 'public',
  ), 
  
  'db2' =&gt; array(
    'driver'   =&gt; 'pgsql',
    'host'     =&gt; 'localhost',
    'database' =&gt; 'db2',
    'username' =&gt; 'root',
    'password' =&gt; 'password',
    'charset'  =&gt; 'utf8',
    'prefix'   =&gt; '',
    'schema'   =&gt; 'public',
  ),
  </code></pre>
  
  <p>So by default <code>db1</code> 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</p>
  
  <pre><code>public function postChangeDb()  {
      $db = Input::get('db');
      Config::set('database.default', $db);
  }
  </code></pre>
  
  <p>Once this is done, I 'refresh' the page, but the connection is still at 'db1'.</p>
  
  <p>I also tried the following</p>
  
  <pre><code>  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!
    }
  </code></pre>
  
  <p>And the above works fine and it successfully switches the database. Is the switch 'per request' basis? </p>