Php 如何知道postgres函数执行时是否没有错误或失败

Php 如何知道postgres函数执行时是否没有错误或失败,php,postgresql,laravel,laravel-5,Php,Postgresql,Laravel,Laravel 5,我用的是拉威尔5.5 我的目标是在postgres功能成功执行或功能失败时在刀片上显示消息 public function archiver(Request $request) { $archivage = DB::select('SELECT archivage()'); if(???) { // successful return redirect()->back()->with('message', "L'archivage

我用的是拉威尔5.5

我的目标是在postgres功能成功执行或功能失败时在刀片上显示消息

public function archiver(Request $request) {

    $archivage = DB::select('SELECT archivage()');

    if(???) {
        // successful
        return redirect()->back()->with('message', "L'archivage a bien été effectué");
    }

    // failed
    return redirect()->back()->with('messageDanger', "Un problème a été rencontré lors de l'archivage");
}
我可以使用
try/catch


谢谢你的帮助

您可以使用
try/catch
和catch
QueryException

try {
  // your function here 
} catch(\Illuminate\Database\QueryException $e) {
 // it's better if you don't use return here and log the error instead like this
  logger()->error($e->getMessage());
  // in this way you can find the log in your storage/logs/laravel.log file
}

它不会引发异常,因此如果您希望返回某个内容,如果(!empty($archivage)){就足够了。使用
empty()
即使我的postgres函数不返回任何内容也有效?然后按照逻辑建议选中
!empty
。如果
archivage()
始终返回空,然后修复它以返回某些内容:/@lawrencerone更改函数行为对我没有帮助。我会看看我能做些什么。感谢您的回答。但是使用
logger()->error($e->getMessage());
将显示错误,对吗?这对用户来说不是很好,或者我错了?它不会向用户显示错误,但我会将其存储在logI see中!但是,如果我不使用
return…
,我如何在用户处显示一条消息来通知他出现了问题?如果您愿意,可以同时使用它们,这会更有帮助ful in-loop你可以先添加记录器,然后将错误消息返回给用户哦,好的,所以我会同时使用这两个!非常感谢!