Laravel多数据库,一个DB存储用户

I have an standard Laravel application I built for a company. I am now making this into a SAAS model but curious the best setup. I know for sure I would want each company to have its own DB for security, easier ability to get specific client data and since these are all competing companies it might just be a nicer selling point.

My issue is how I can set up a but of DBs and sub domains to point to their specific DB and application set up but I dont really want 100 sub domains and for an iOS application I need one single user auth portal.

Is there a specific way to have a db that holds all the laravel users and on login it gives them to their specific DB? Basically I just need one portal (auth location).

I know this is generic but not sure where to even post this or what to search for.

Setup:

user db (standard laravel setup)

User | Password Reset | Migrations | Roles

company a db

accounts | routes

company b db

accounts | routes

You have a default database for your application. That's where user authenticate. Each company will then have their db details stored on your main database. As soon as someone authenticates, you take the db details and make it as default.

public function setDefaultConnection($company)
{
    //update the config
     config(['database.connections.mysql' => [
        'host'     => $company->host,
        'username' => $company->username,
        'password' => $company->password
    ]]);

    //Check the credentials by calling PDO 
    try {
        DB::connection()->getPdo();
    } catch (\Exception $e) {
        return redirect()->back()->withErrors(["connection" => "Could not connect to the database.  Please check your input."]);
    }
}

Not sure it's the best approach, but it lets you at least change connection details on the fly.

On the other hand, you could avoid updating existing and actually add a new one on the fly

config(['database.connections.'.$company->id => [
    'driver'   => 'mysql',
    'host'     => $company->host,
    'username' => $company->username,
    'password' => $company->password
]]);

//Using it
$clients = DB::connection($company->id)->select(...);

use join table method for multi-database with one DB storing users

learn more about laravel join table Click here