Hi everyone, I'm dealing with this for a long and I really can't find a solution, I'm creating a RESTful API derived from an old project I have. So I have defined the rules of validation in several form requests, and I want to use the same form requests to validate inputs in my API. So for example
public function store(PatientRequest $request){
Patient::create($request->all());
return response()->json([
'message' => 'Saved successfully'
], );
}
The problem here is that I cannot trigger a json response in case the validation fails. Any thoughts?
If you are submittin the request outside a HTML Form than you have to set your HTTP request headers as follow:
Content-Type: application/json Accept: application/json
In this way, Laravel will give you back the validation errors in json format and HTTP status 422 (Unprocessable Entity)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community