title: How to change database name dynamically before authenticate in laravel
body: <p>I have one application. in that, in login page along with username and password fields, there is another field company selection drop-down. like (comp1, comp2,comp3).
  based on company selection I want to connect to that company related database.
  all databases having the same tables. just I want to change database name and handle with that database throughout the app.</p>
  
  <p>for that, I created on BeforeMiddleware. in that added the following code.</p>
  
  <pre><code>namespace App\Http\Middleware;
  
  use Closure;
  use Illuminate\Support\Facades\DB;
  use Illuminate\Support\Facades\Config;
  
  class BeforeMiddleware
  {
      public function handle($request, Closure $next)
      {
  
          $company_name = $_GET['company_name'];
  
          Config::set('database.connections.mysql.database', $company_name );
          DB::purge('mysql');
          DB::reconnect();
  
          if(DB::connection()-&gt;getDatabaseName())
          {
  
              echo "Connected sucessfully to database ".DB::connection()-&gt;getDatabaseName().".";
  
          }
  
          return $next($request);
      }
  } 
  </code></pre>
  
  <p>here the selected database is connected successfully but after i am getting 
  <code>FatalErrorException Call to a member function prepare() on null</code> error.</p>
  
  <p>please let me know how to change database name dynamically in single place</p>