2017-10-10 52 views
0

大家,前幾天,我問一個可能的解決方案Try和Catch在一個函數中,我通過$ id,但現在,我有類似的問題,現在,我不' t需要傳遞任何$ id,只是一個視圖,該視圖有一個從我的應用程序的管理面板上傳的文件(PDF),我試圖說明一些管理員刪除文件(PDF),並且用戶嘗試訪問頁面的條款和條件沒有任何PDF上傳,系統重定向到其他頁面,如家;這是我的代碼:

public function terms() 
    { 
    try { 
     $terms = Multimedia::where('multimedia_type', 'terms')->first(); 
     return view('terms.terms', compact('terms')); 
    } catch (\Exception $e) { 
     return redirect('/')->with('errors', 'Ha ocurrido un errror, lo sentimos'); 
    } 
    } 

但是,當我刪除的文件,我得到這個錯誤:

(2/2) ErrorException Trying to get property of non-object (View: /home/vagrant/Code/Biblio/resources/views/terms/terms.blade.php)

而且系統也沒有重定向到其他頁面。如果有人能幫忙,我會非常感謝

+0

terms.blade.php中存在哪些代碼? –

回答

1

錯誤來自刀片模板。在這種情況下,文件不存在,它會突破try catch。 PHP假定您將null傳遞到模板中。

爲了解決這個問題,你可以做到以下幾點:

if($terms) { 
    return view('terms.terms', compact('terms')); 
} else { 
    return redirect('/')->with('errors', 'Ha ocurrido un errror, lo sentimos'); 
} 

注意,你可以做到這一點的功能的嘗試捕捉的外剛宣佈$terms因爲你已經離開。

+0

這是正確的答案,謝謝你,你真的幫助我! –