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

[8.x] Ability to return the default value of a request whenHas and whenFilled methods #38060

Merged
merged 3 commits into from
Jul 19, 2021

Conversation

olekjs
Copy link
Contributor

@olekjs olekjs commented Jul 19, 2021

Many times I needed to return the default value using one of the whenHas or whenFilled methods, but I could not which was annoying because such a great method was practically useless in some cases.

The solution taken from the collection, we have there for example the whenEmpty method, which takes a paramter that returns the default value.

Let's see how this is used in collections:

$collection->whenEmpty(function ($collection) {
    return $collection->push('Adam');
}, function ($collection) {
    return $collection->push('Taylor');
});

In the same way now it would be possible to obtain in the request object:

$request->whenHas('name', function ($name) {
    return $name;
}, function () {
    return 'some default value';
});

If we do not pass the default value, we will get an instance of the request class, which is the same as before the changes I made.

If I was the only person who needed this solution, then PR is to close.

I also added tests and I hope that I considered each case, I tried to test the introduced code so that it does not force changes on existing solutions.

@GrahamCampbell GrahamCampbell changed the title Ability to return the default value of a request whenHas and whenFilled methods [8.x] Ability to return the default value of a request whenHas and whenFilled methods Jul 19, 2021
@taylorotwell
Copy link
Member

You have changed the entire behavior of the function regarding returning $this, etc. Please correct that.

@olekjs
Copy link
Contributor Author

olekjs commented Jul 19, 2021

@taylorotwell You're right. I already changed the code. Now there is only support for the default value. In addition, I removed the value parameter from default, because it will always be null, so it was unnecessarily there.

Tests have also been slimmed down, if anything else will be to correct let me know!

@taylorotwell taylorotwell merged commit c7824d4 into laravel:8.x Jul 19, 2021
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.

2 participants