Ask Your Question
0

What is the process of altering the default database when logging in and forwarding all session requests to that database within a Laravel application?

asked 2021-09-03 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-01-06 09:00:00 +0000

pufferfish gravatar image
  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.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-03 11:00:00 +0000

Seen: 7 times

Last updated: Jan 06 '23