0

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();
Prashant Patel
  • 1,087
  • 11
  • 18
  • check this first : https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel – NIKUNJ_ACE May 07 '19 at 12:09
  • 3
    Possible duplicate of [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – Nico Haase May 07 '19 at 12:20

0 Answers0