Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
phh:request-when-missing
Nov 19, 2022
Merged

[9.x] Add whenMissing() method to InteractsWithInput trait #45019
taylorotwell merged 1 commit into
laravel:9.xfrom
phh:request-when-missing

Conversation

@phh

@phh phh commented Nov 18, 2022

Copy link
Copy Markdown
Contributor

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
Comment thread src/Illuminate/Http/Concerns/InteractsWithInput.php
@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.

3 participants