Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 破坏函数laravel中的尝试捕获方法_Php_Laravel_Try Catch - Fatal编程技术网

Php 破坏函数laravel中的尝试捕获方法

Php 破坏函数laravel中的尝试捕获方法,php,laravel,try-catch,Php,Laravel,Try Catch,当您在数据库中没有要删除的内容时,我会尝试显示一条消息,而不是显示一个表示您有空值的错误 public function destroy($customer_id) { $customer_response = []; $errormsg = ""; $customer = Customer::find($customer_id); $result = $customer->delete(); try{ //retrieve page

当您在数据库中没有要删除的内容时,我会尝试显示一条消息,而不是显示一个表示您有空值的错误

public function destroy($customer_id)
 {

    $customer_response = [];
    $errormsg = "";

    $customer = Customer::find($customer_id);
    $result = $customer->delete();
    try{
    //retrieve page
      if ($result){
          $customer_response['result'] = true;
          $customer_response['message'] = "Customer Successfully Deleted!";

      }else{
          $customer_response['result'] = false;
          $customer_response['message'] = "Customer was not Deleted, Try Again!";
      }
      return json_encode($customer_response, JSON_PRETTY_PRINT);

    }catch(\Exception $exception){
      dd($exception);
        $errormsg = 'No Customer to de!' . $exception->getCode();

    }

    return Response::json(['errormsg'=>$errormsg]);
  }
与我以前的存储功能相比,try/catch方法不起作用。

请进一步阅读。您可以捕获它在找不到时抛出的异常

try {
    $customer = Customer::findOrFail($customer_id);
} catch(\Exception $exception){
    dd($exception);
    $errormsg = 'No Customer to de!' . $exception->getCode();
    return Response::json(['errormsg'=>$errormsg]);
}

$result = $customer->delete();
if ($result) {
    $customer_response['result'] = true;
    $customer_response['message'] = "Customer Successfully Deleted!";
} else {
    $customer_response['result'] = false;
    $customer_response['message'] = "Customer was not Deleted, Try Again!";
}
return json_encode($customer_response, JSON_PRETTY_PRINT);
进一步阅读。您可以捕获它在找不到时抛出的异常

try {
    $customer = Customer::findOrFail($customer_id);
} catch(\Exception $exception){
    dd($exception);
    $errormsg = 'No Customer to de!' . $exception->getCode();
    return Response::json(['errormsg'=>$errormsg]);
}

$result = $customer->delete();
if ($result) {
    $customer_response['result'] = true;
    $customer_response['message'] = "Customer Successfully Deleted!";
} else {
    $customer_response['result'] = false;
    $customer_response['message'] = "Customer was not Deleted, Try Again!";
}
return json_encode($customer_response, JSON_PRETTY_PRINT);

Customer::findOrFail($Customer\u id)会自动生成404响应。问题是我想显示我自己的错误消息谢谢
Customer::findOrFail($Customer\u ID)将自动生成404响应。问题是我想显示我自己的错误消息,谢谢