That should work.
Simple test case. If you place this somewhere in a controller and go to: your-page?foo=bar&otherfield=notalllowed what do you see?
$data = request()->validate([
'foo' => 'nullable',
]);
dd($data);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So sometimes I call my validation after a few checks, so i have a FormRequest with the rules and made a static function in there that return the rules.
But when I do
$data = $request->validate(CostCreate::getRules());
return $data;
I get more data than expected, I expect only the validate data but instead I get ALL the columns.
Any idea how I get only the validated data?
See this ticket on SO https://stackoverflow.com/questions/50317448/how-to-validate-an-array-in-laravel-and-return-its-filtered-values
Same question in 2013, no solution provided. And there still is no solution (not by default at least).
protected function extractInputFromRules(Request $request, array $rules)
{
return $request->only(collect($rules)->keys()->map(function ($rule) {
return Str::contains($rule, '.') ? explode('.', $rule)[0] : $rule;
})->unique()->toArray());
}
If you have nested data there is no whitelist on fieldnames. If you want that you should do it manually.
A simple solutions could be to use array_only($inputArray, ['name', 'type', 'amount', 'price']) on each array element. Another solution would be is to extend/overwrite the behavior of above method in the ValidatesRequests trait.
Please or to participate in this conversation.