-1

I need my system to connect to different databases simultaneously.

I have a general database that stores the user's connection data (db_user, db_name, db_password), these parameters are what I would send to my system so that it connects with its respective database and displays its information. The detail is when several users log in, my configuration information would be changing and I need to keep it in all possible connections. I have not yet implemented any solution so I receive your recommendations. I hope it has been explained in the best way, thank you very much.

  • 2
    Try reading the [documentation](https://laravel.com/docs/5.8/database#configuration) or at least [Google](https://fideloper.com/laravel-multiple-database-connections) it your question before posting it on Stackoverflow. – Thomas Van der Veen Jun 04 '19 at 17:32

2 Answers2

0

You can use the following:

DB::connection('database_name')->query
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
msbomrel
  • 521
  • 6
  • 19
0

add new database connection to config/database.php

'mysql' => [
    'driver'    => env('DB_CONNECTION'),
    'host'      => env('DB_HOST'),
    'port'      => env('DB_PORT'),
    'database'  => env('DB_DATABASE'),
    'username'  => env('DB_USERNAME'),
    'password'  => env('DB_PASSWORD'),
],

'mysql2' => [
    'driver'    => env('DB_CONNECTION_SECOND'),
    'host'      => env('DB_HOST_SECOND'),
    'port'      => env('DB_PORT_SECOND'),
    'database'  => env('DB_DATABASE_SECOND'),
    'username'  => env('DB_USERNAME_SECOND'),
    'password'  => env('DB_PASSWORD_SECOND'),
],

and you can use the following:

DB::connection('mysql2')->select(...);
Ali Ghaini
  • 882
  • 6
  • 13