2017-10-10 54 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

这是正确的答案,谢谢你,你真的帮助我! –