如何仅从 laravel FormRequest 中获取经过验证的数据?
2022-08-30 20:48:10
假设我有以下自定义请求:
class PlanRequest extends FormRequest
{
// ...
public function rules()
{
return
[
'name' => 'required|string|min:3|max:191',
'monthly_fee' => 'required|numeric|min:0',
'transaction_fee' => 'required|numeric|min:0',
'processing_fee' => 'required|numeric|min:0|max:100',
'annual_fee' => 'required|numeric|min:0',
'setup_fee' => 'required|numeric|min:0',
'organization_id' => 'exists:organizations,id',
];
}
}
当我从控制器访问它时,如果我这样做,它会给我所有的数据,包括额外的垃圾数据,这些数据不是要传递的。$request->all()
public function store(PlanRequest $request)
{
dd($request->all());
// This returns
[
'name' => 'value',
'monthly_fee' => '1.23',
'transaction_fee' => '1.23',
'processing_fee' => '1.23',
'annual_fee' => '1.23',
'setup_fee' => '1.23',
'organization_id' => null,
'foo' => 'bar', // This is not supposed to show up
];
}
如何仅获取经过验证的数据而不手动执行?$request->only('name','monthly_fee', etc...)