Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  1. Open the config/database.php file in your Laravel application.
  2. Look for the 'default' key in the 'connections' section of the file.
  3. Change the value of the 'default' key to the name of the database you want to use as the default.
  4. Save the database.php file and close it.
  5. Open the .env file in your Laravel application.
  6. Look for the 'DB_DATABASE' key and change its value to the name of the database you want to use as the default.
  7. Save the .env file and close it.
  8. Open the app/Providers/AppServiceProvider.php file in your Laravel application.
  9. In the register() method of the file, add the following code:

    use Illuminate\Support\Facades\DB;
    
    public function register()
    {
       $request = app('request');
    
       if ($request->server('HTTP_HOST') == 'yourdomain.com') {
           config(['database.default' => 'yourdatabase']);
           DB::reconnect();
       }
    }
    

    This code checks the HTTP host and changes the default database and reconnects to it.

  10. Save the AppServiceProvider.php file and close it.
  11. Test your application to confirm that it's now sending all session requests to the new default database.