if(DB::connection()->getDatabaseName()){
$data = User::get(['id','first_name','last_name']);
return View::make('index')->with('data',$data);
}
else{
$request->session()->put('error','Could not connect to the database. Please check your configuration.');
return view::make('errors.503');
}
我的 else
部分在数据库未连接时不工作。如何在 laravel 5.2 中处理所有 DB 连接和异常异常
最佳答案
DB::connection()->getDatabaseName()
从您的配置中检查名称,这一定是为什么总是评估为 true 的原因
试试 DB::connection()->getPdo();
,如果失败会抛出异常(使用 try/catch)。
编辑:
try {
DB::connection()->getPdo();
$data = User::get(['id','first_name','last_name']);
return View::make('index')->with('data',$data);
} catch (\PDOException $e) {
$request->session()->put('error','Could not connect to the database. Please check your configuration.');
return view::make('errors.503');
}
关于php - 数据库连接和模具问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43831422/