Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add whenMissing() method to InteractsWithInput trait #45019

Merged
merged 1 commit into from Nov 19, 2022
Merged

[9.x] Add whenMissing() method to InteractsWithInput trait #45019

merged 1 commit into from Nov 19, 2022

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants