Skip to content

Conversation

phh
Copy link
Contributor

@phh phh commented Nov 18, 2022

The InteractsWithInput methods has() and filled() both have a syntactic sugar method whenHas() and whenFilled() that accepts a callback to avoid adding if's.

This PR adds the method whenMissing() that is basically the opposite of whenHas().

Today I was working on a middleware that takes care of both mappings and default fields. That looked something like this:

$request->mergeWhenMissing([ 'type' => 'default', ]);

However, sometimes you need to add a little more than just the additional fields. Could be something like notifying a 3rd part service that this part of the API is still getting used:

if ($request->whenMissing('type') { $this->deprecated('type'); $request->merge(['type' => 'default']); }

I just think it's a nice little syntactic sugar.

@phh phh changed the title [7.x] Add whenMissing() method to InteractsWithInput trait [9.x] Add whenMissing() method to InteractsWithInput trait Nov 18, 2022
@taylorotwell taylorotwell merged commit e19dbdb into laravel:9.x Nov 19, 2022
@phh phh deleted the request-when-missing branch November 22, 2022 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants