Accessing dynamic database in laravel

Posted 2 years ago by codeistalk

HI

I am working on an application. I have a master database, where I store all database names. So I have made an api, which sends db_name, so if that db_name exists in master db. I fetch that value and it's credentials.

Now I after that I want to perform all my operations on that new database.

I have written controller function like this, but this seems not working:

 public function test(Request $request) {

        $users = Config::where('db_name', '=', $request->db_name)->first();

        if ($users) {
            config(['database.mongodb' => [
                'database' => $users->db_name
            ]]);
        }

        $contacts = \DB::collection('contacts')->get();

        return response()->json($contacts);

    }

Any suggestions?

TIA

Please sign in or create an account to participate in this conversation.