I'm bulding an exception log into my Laravel app to send mails or Telegram messages when an error identified as important is dispatched.
Actually, I'm using a 'try catch' block on each of my controller functions to catch an exception dispathced by any part of my app.
My question is, how does Laravel validation works to return exceptions as JSON response without using return
sentences or try catch
blocks on each call?
What I must do now into controller:
someAction();
try {
Model::someFunction($params);
catch (MyCustomException $e)
return json()->response($e->getMessage(), 500); // Return json error if fails
}
return anotherAction();
What I want to do into controller:
someAction();
Model::someFunction($params); // Return json error if fails and stop execution
return anotherAction();
What Laravel validation makes:
someAction();
$request->validate($validationRules); // Return json error if validation fails and stop execution
return anotherAction();