【发布时间】:2018-04-27 22:33:33
【问题描述】:
我一直在尝试将带有规则和消息的FormRequest 添加到我的删除方法中,但请求返回为空并且规则每次都失败。
是否可以通过delete方法获取请求数据?
这是我的请求类:
use App\Http\Requests\Request;
class DeleteRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'staff_id' => ['required', 'exists:users,uid'],
'reason' => ['required', 'string'],
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [
'staff_id.required' => staticText('errors.staff_id.required'),
'staff_id.exists' => staticText('errors.staff_id.exists'),
'reason.required' => staticText('errors.reason.required'),
'reason.string' => staticText('errors.reason.string'),
];
}
}
还有控制器:
/**
* Handle the 'code' delete request.
*
* @param integer $id The id of the code to fetch.
* @param DeleteRequest $request The request to handle the data.
* @return response
*/
public function deleteCode($id, DeleteRequest $request)
{
dd($request->all());
}
【问题讨论】:
-
您好,请出示您的代码。因为可以通过任何方法获取请求数据。也许在删除方法中,您不会将 FormRequest 对象作为参数传递。
-
使用 Postman 进行测试。如果我将数据作为查询参数而不是作为表单主体传递,它会起作用。
-
也许您的 DeleteRquest 类需要扩展 FormRequest,因为这是使用请求表单(在视图中使用)的正确类。使用postman测试时,在form body配置中尝试过去的值。因为这与 laravel 视图中使用的形式相同。
-
我的自定义 Request 类对所有其他请求都非常有效,所以我没有理由相信它会因为这个而失败。而且我已经用默认的FormRequest类测试过了,请求数据还是不存在的。
标签: php laravel http laravel-5 http-delete