I am using Laravel on the server side. Imagine that our controller receives two fields url[string] and data[array with index head]. We can verify data and customize error messages using
$this->validate($request, [
'url' => 'required',
'data.head' => 'required',
], [
'url.required' => 'The :attribute field is required',
'data.head.required' => 'The :attribute field is required',
]);
If the check is not completed, Laravel sends a response with json data
{
"url": ["The url field is required"],
"data.head": ["The data.head field is required"]
}
How can we convert the response data to send json as below?
{
"url": ["The url field is required"],
"data": {
"head": ["The data.head field is required"]
}
}
source
share