在单个数组中获取laravel验证异常消息



我希望所有Laravel验证异常消息都在一个数组中。目前,我收到以下回复:

{
"message": "The given data was invalid.",
"errors": {
"name": [
"The name has already been taken."
],
"title": [
"The title has already been taken."
],
}
}

我需要它如下:

{
"message": "The given data was invalid.",
"errors": [
"The name has already been taken.",
"The title has already been taken."
]
}

你可以试试这个

$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body'  => 'required',
]);
if ( $validator->fails() ) {
return response()->json([
'message' => 'The given data was invalid.',
'errors'  => $validator->errors()->all()
]);
}

您可以使用所有函数在单个数组中获取错误消息

$validator->errors()->all();

相关内容

  • 没有找到相关文章

最新更新