如何仅从 laravel FormRequest 获取经过验证的数据?
How do I get ONLY the validated data from a laravel FormRequest?
假设我有以下自定义请求:
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()
,它会给我 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...)
的情况下仅 获得验证数据?
$request->validated()
将 return 只有经过验证的数据。
示例:
public function store(Request $request)
{
$request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
$validatedData = $request->validated();
}
替代解决方案:
$request->validate([rules...])
return如果验证通过,则为唯一验证数据。
示例:
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
好的...在我花时间打完这个问题之后,我想我应该查看 laravel "API" 文档:https://laravel.com/api/5.5/Illuminate/Foundation/Http/FormRequest.html
看来我可以使用 $request->validated()
。希望他们会在验证文档中这样说。它使我的控制器动作看起来非常流畅:
public function store(PlanRequest $request)
{
return response()->json(['plan' => Plan::create($request->validated())]);
}
假设我有以下自定义请求:
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()
,它会给我 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...)
的情况下仅 获得验证数据?
$request->validated()
将 return 只有经过验证的数据。
示例:
public function store(Request $request)
{
$request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
$validatedData = $request->validated();
}
替代解决方案:
$request->validate([rules...])
return如果验证通过,则为唯一验证数据。
示例:
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}
好的...在我花时间打完这个问题之后,我想我应该查看 laravel "API" 文档:https://laravel.com/api/5.5/Illuminate/Foundation/Http/FormRequest.html
看来我可以使用 $request->validated()
。希望他们会在验证文档中这样说。它使我的控制器动作看起来非常流畅:
public function store(PlanRequest $request)
{
return response()->json(['plan' => Plan::create($request->validated())]);
}