我希望我的网站连接到另一个数据库,如果当前的数据库不可用或找不到,则使用与当前数据库相同的凭据。
答案 0 :(得分:1)
您可以使用查询尝试连接,如果失败则更改连接。
try {
User::find(1); // or any other SQL query you want to check
} catch (\Exception $e) {
config(['database.connections.data' => array( // you don't have to add the connection runtime but it can be done.
'driver' => 'sqlsrv',
'host' => $connection['Database_Server'],
'database' => $connection['Database_Name'],
'username' => $connection['Database_User'],
'password' => $connection['Database_Pass'
)]);
DB::setDefaultConnection('data'); // change the default connection