使用laravel进行云实施

I have build an application which is not yet a implemented to a cloud, however I am planing to change its architecture to have cloud features. I am planing to have one application with every client a different/client specific database. I tried to setup multiple database in config/database.php and set the default connection to my system database. I am planing to have 2 tables in this database, one is users table (for authentication) and client table, for getting the client database information (database name, username, password etc). Once the authentication is done, i will grab the client details and connect to client database, I want all other operations after authentication to happen on that connection. I tried to create a connection on fly using the following code, once authentation is done

DB::disconnect(); /* disconnecting system database connection
and creating a new connection dynamically based on the values i get from 
clients table */

config::set(['database.connections.on_fly' => [
        'driver'    => $params['driver'],
        'host'      => $params['host'],
        'database'  => $params['database'],
        'username'  => $params['username'],
        'password'  => $params['password'],
    ]]);

    Config::set('database.default','on_fly');
    return DB::reconnect();

But this doesnt work for me, since the code works for the current reuest only, once this reuest is done, the system automatically sets the default database back to system. Can anyone suggest me a good approach, or implementation that i can consider to achieve the reuirement.

Thanks in advance

Try to add the code in a service provider boot method and let me know whether it works? and rather you replace an existing connection template, try to create a new connection config at run time and naming the connection template with the prefix of your tenant_id, which is appropriate and will work for sure, example:

$config['database'] = $tenant->database_name;
$config['username'] = $tenant->database_username;
$config['password'] = $tenant->database_password;

Config::set('database.connections.tenant_'.$tenant->id, $config);
Config::set('database.default', 'tenant_'.$tenant->id);
DB::reconnect('tenant');

you may also refer to this article, it might be helpful, you can also ask your support in the comment under article, https://www.techalyst.com/links/read/139/laravel-database-per-user-on-the-fly-database-connections-multi-tenant-laravel-app-user-own-db