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');
}
当DB未连接时,我的else
部分无效。如何在laravel 5.2中处理所有数据库连接和死亡异常
答案 0 :(得分:1)
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');
}