2017-05-07 87 views
1
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部分在DB未連接時不工作。如何處理所有的數據庫連接,並從配置在laravel死例外5.2數據庫連接和管芯問題

回答

1

DB::connection()->getDatabaseName()檢查名字,那必須是爲什麼總是計算爲真

之所以會轉而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'); 
} 
+0

我嘗試(try/catch語句)。但不能解決問題你能告訴我try/catch的正確方法嗎? @vivoconunxino –

+0

'這個頁面不能正常工作 localhost重定向你太多次了。 嘗試清除您的Cookie。 ERR_TOO_MANY_REDIRECTS'此錯誤即將到來。 –

+0

以上解決方案無法正常工作。 @vivoconunxino –