I'm using form request for validation on an which automatically returned a json of errors when I have one
{
"message": "The email address has already been taken.",
"errors": {
"email_address": [
"The email address has already been taken."
]
}
}
How can I add a custom key and value inside this json before the response e.g. "status": false
{
"status": false,
"message": "The email address has already been taken.",
"errors": {
"email_address": [
"The email address has already been taken."
]
}
}
@brainyt Why? If you’re getting a validation error response then the status is clearly “false”. You don’t need to add that in the response body. This is literally what HTTP status codes are for.
If you get a 4XX status code, then you have an error. Laravel returns a 422 status code for validation failures.
@martinbean I have tried to explain this to other team members, but still insist I have the status there. Though I have found my way around it. Thanks for always coming for us.
@BrainyT I don't disagree with @martinbean - but anyway handling the Validation exception yourself in the App\Exceptions\Handler class (shown above) will allow you to achieve this.