所以基本上我正在构建一个客户想要连接到他自己的数据库的应用程序,我制作了一个表格,他可以在其中提交他的凭据
connectionForm: this.$inertia.form({
driver: 'mysql',
name: '',
host: '',
port: 3306,
username: '',
password: '',
database: '',
charset : 'utf8',
collation: 'utf8_unicode_ci',
prefix: '',
})
laravel Controller 中接收到的数组
array:10 [▼
"driver" => "mysql"
"name" => "Connection"
"host" => "127.0.01"
"port" => 3306
"username" => "root"
"password" => null
"database" => "clinic"
"charset" => "utf8"
"collation" => "utf8_unicode_ci"
"prefix" => null
]
我想创建一个函数来检查提交的数据库凭据是否可以连接到他的数据库,到目前为止我还没有找到通过传递凭据数组来完成此操作的方法。我试过这种方式
if (DB::connection($request->validated())->getDatabaseName())
{
ray('The connection was successfull');
return 'Connected to the DB: ' . DB::connection('myDamnDbConnection')-getDatabaseName();
}
但它需要一个字符串,而不是一个数组。 有什么办法吗?
最佳答案
将数据库配置作为新条目添加到您的配置中,然后使用它:
config([ 'database.connections.temp' => $request->validated() ]);
DB::connection('temp')->connect(); // Would throw an exception if it fails
关于php - 如何检查数据库连接数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68789422/