Hi @samcogan, I think wich you can use this:
response()->json($yourArray, 200);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I may be missing something obvious here, but I am struggling to find a way to do this. I am using Form Request's to validate my forms and they work great, I am now implementing an API for my app which also needs to validate the data, with the same rules as my forms, so it seems sinsible to re-use the form requests we already created, rather than duplicating these rules. However I can't find much documentation on how to do this, and get the validation errors output as JSON. Am I missing something and this is a bad idea?
What I have done so far is to be able to get the validation object in my API controller and check if validation fails or not, but I can't find a way to display the messages as JSON.
So in my request I have added this:
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
$this->validator = $validator;
}
and then in the controller I can access like this:
public function store(AirportRequest $request)
{
if (isset($request->validator) && $request->validator->fails()) {
}
}
But how do I output the the results as JSON? I am using the ellipsesynergie\api-response project for formatting output, so I really just need to get an array of the error messages that can be converted to JSON.
Please or to participate in this conversation.