Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Garet's avatar
Level 3

Remove items from $request->validated()

Normally, you could update a model using the validated data from a form request:

public function update(Item $item, StoreItem $request)
{
    $item->update($request->validated());
    return redirect()->back();
}

However, occassionally, I might want to validate data that is part of the model, but also data that isn't part of the model. For example, let's say I had a tick box called "I agree to updating these details" and I want to validate that the tick box is checked, but this isn't part of the model.

To do that, I have to resort to the following:

public function update(Item $item, StoreItem $request)
{
    $data = collect($request->validated())->forget('tickbox_agree')->toArray();
    $item->update($data);
    return redirect()->back();
}

Basically, I convert $request->validated from an array to a collection, remove the element in question, then convert it back to an array. It works, but is there a better way?

Thanks,

0 likes
13 replies
bobbybouwmann's avatar

There is no one simple way to achieve what you want here because validated returns an array.

I would probably do it the other way around and only select the fields you need from the request

$item->update($request->only(['field1', 'field2']));

Or the other way around

$item->update($request->except(['agree']));
1 like
tykus's avatar

There is the option to use Laravel's Arr class also:

$item->update(Arr::only($request->validated(), ['field1', 'field2']));

or, the opposite

$item->update(Arr::except($request->validated(), 'field3'));

No need for a Collection object in this example, but the API is similar.

Garet's avatar
Level 3

Hi both,

Thanks for your reply.

@bobbybouwmann - thanks for yuor suggestions. The problem with using $request->only() is that in some cases there can be dozens of fields, so it's quite messy to specify them all outside of the FormRequest class. Likewise, $request->except() means we need to populate the model's $fillable attribute, whereas we don't have to do this when using $request->validated()

@tykus - thanks, I think your suggestion is a little cleaner than mine. I'll go with that for now.

I wish there was a $request->validatedExcept() method.

tykus's avatar
tykus
Best Answer
Level 104

I wish there was a $request->validatedExcept() method.

The Request class is macroable 😉, e.g.

Request::macro('validatedExcept', function ($except = []) {
	return Arr::except($this->validated(), $except);
});
3 likes
jlrdw's avatar

Just skip the validation on the check box, and use if request has method, then do what you need to do.

Meaning only use the if request has for the checkbox.

GeordieJackson's avatar

@garet

In the FormRequest class you can use the prepareForValidation() method to do stuff before the validation runs. So,you could do something like:

protected function prepareForValidation()
{
    if( $this->missing('tickbox_agree')) {
        back()->withErrors('.....');
    }
}

and leave this check out of your validation rules. Then it won't spoil your validated array.

1 like
jlrdw's avatar

Or maybe even be better to store that as a 1, for archival reasons to show that they did agree.

Snapey's avatar

can't you unset it?

    $data = collect($request->validated());

    unset($data['tickbox_agree']);

    $item->update($data);

    return redirect()->back();
GeordieJackson's avatar

Another simple solution is to use Arr::except() in the Rules() method:

public function rules()
{
    return Arr::except([
        $validate
    ], [$except]);
}

Then you wouldn't have to pass in the exception keys from the controller.

Garet's avatar
Level 3

Thanks for the suggestions everyone, so many ways to peel an orange.

I went with the macro solution by @tykus because it's closest to what I desired prior to knowing it was possible. I'll copy this macro to future projects and can now use $request->validatedExcept() without thinking about it.

SilverPaladin's avatar

$item->update( Arr::except(array: $request->validated(), keys: ['tickbox_agree']));

Garet's avatar
Level 3

@SilverPaladin Thanks for your reply. This question was 4 years ago, since then Laravel has included the safe() method which allows you to do this:

$request->safe()->except(['tickbox_agree']);

And also

$request->safe()->only(['first_name', 'last_name']);

Please or to participate in this conversation.