流明检查数据库配置没有提示错误

Lumen check database configured without prompting error

我的 lumen 应用程序连接到多个数据库,我的功能之一需要通过 URL 参数连接到数据库,有什么方法可以检查我的 config/database.php 中是否已经存在数据库?

我试过使用下面的功能:

$client = $request->input('client');
if (!$databse->setConnection($client)->getDatabaseName()) {
    return 'no db';
}

但是如果数据库不存在会提示错误,即使我能catch InvalidArgumentException来显示错误信息,但是有没有其他方法可以做到这一点?谢谢!

您可以使用 Config 外观中的 has 方法来检查连接是否已配置:

use Illuminate\Support\Facades\Config;

...

if(Config::has('database.connections.client_connection')) {
  echo "The database exists.";
} else {
  echo "Please create the database first!";
}