如何在laravel 5.7中一起使用Auth和Config?

In my project, I need to use multiple databases depending on each user where users table is in separate database.

First I get users database using Auth->user()->db and pass it to Config::set(). But, when I try to get data from the new database, Laravel tries to connect to the first one.

How can i fix it? The users database is set in env file.

$db= Auth::user()->name;

Config::set('database.connections.sqlsrv', [
    'driver' => 'sqlsrv',
    'host' => 'localhost',
    'port' => '1433',
    'database' => $db
]);

$query = DB::connection('sqlsrv');
$posts = $query->table('posts')->get();