Laravel 5.8.0 is used. There are several databases whose data are populated from the admin panel. The standard config (/config/database.php), of course, does not get the data, but is recorded and stored in a separate JSON-config (/storage/app/databases.json).
The question is the following: How can I connect to a third-party database not by entering data into the config manually, but using my own automatically received data for connection?
I know about the existence of setConnection() . I tried to write something like this:
$servers = conf()->get('servers'); //Пакет garf/laravel-conf, тут у меня массив с информацией по базам foreach($servers as $server){ if(empty(Config::get("database.connection.".$server['name_database']))){ Config::set( // И через config() тоже работать не будет 'database.connection.'.$server['name_database'], [ 'driver' => 'mysql', 'host' => $server['ip_database'] ?? '127.0.0.1', 'port' => $server['port_database'] ?? 3306, 'database' => $server['name_database'], 'username' => $server['user_database'], 'password' => $server['pass_database'], 'unix_socket' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, ] ); } $punishment = new Punishment; $punishment->setConnection($server['name_database']); $list = $punishment->where('name', '=', Auth::user()->name); } But Laravel damn it for two wants to put something into its config programmatically, so I get the error:
InvalidArgumentException Database [server_classic] not configured.
PS Just to write the connection directly (through the same PDO) does not see the point. And about “use one base” not to write, it will not work that way, because third-party databases are used locally on game servers and for servers, and you only need to receive some information from time to time.