1

Im trying display a message when you have nothing to delete in the database instead of showing a error that says you have a null value

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]);
  }

the try/catch method is not working compared to my previous store function that is working

2
  • 1
    Customer::findOrFail($customer_id); generates a 404-response automatically if this ID does not exist. Commented Dec 13, 2016 at 5:19
  • the thing is i want to dislay my ownerror message thank you Commented Dec 13, 2016 at 5:23

1 Answer 1

7

Read up further on findOrFail. You can catch the exception it throws when it fails to find.

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.