我正在尝试使用 Laravel 验证来生成自定义错误消息,但是我找不到应该覆盖的函数。
Route:POST:/entries/
用于执行验证EntryController@store
的用途。EntryStoreRequest
入口存储请求
namespace App\Api\V1\Requests;
class EntryStoreRequest extends ApiRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'message' => [
'string',
'required',
'max:65535',
],
'code' => [
'string',
'max:255',
'nullable'
],
'file' => [
'string',
'max:255',
'nullable'
],
'line' => [
'string',
'max:255',
'nullable'
],
'stack' => [
'string',
'max:65535',
'nullable'
]
];
}
}
ApiRequest
namespace App\Api\V1\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class ApiRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
错误当前返回为:
{
"message": "The given data was invalid.",
"errors": {
"message": [
"The message field is required."
]
}
}
我想将它们格式化为:
{
"data": [],
"meta: {
"message": "The given data was invalid.",
"errors": {
"message": [
"The message field is required."
]
}
}
我怎样才能在ApiRequest
课堂上做到这一点?