You can create a custom validator to accomplish this. Here is an example:
$app->post('/user', function (Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required',
]);
return response(
['data' => $validator->messages()]
);
});
Use Validator::make and pass the request and rules. Then you can return your response and wrap the messages however you want. In the example above the response is:
{
"data": {
"name": [
"The name field is required."
]
}
}
Note in order to use the Validator facade you need to uncomment $app->withFacades(); in your bootstrap/app.php file.