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

[5.4] Create a new "every" method on the collection #16777

Merged
merged 4 commits into from Dec 14, 2016

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Dec 13, 2016

Creates a new every method, to check if all items in the collection pass a given test:

$ages = collect([['age' => 18], ['age' => 20], ['age' => 24]]);

$allAdult = $ages->every(function ($item) {
    return $item['age'] >= 18;
});

You can also pass it a single argument as a key:

$allActive = $users->every('active');

You can also pass it a key, operator & value, like you would for the where method:

$ages = collect([['age' => 18], ['age' => 20], ['age' => 24]]);

$allAdult = $ages->every('age', '>=', 18);

Also works with the higher order method proxies:

$allSubscribed = $users->every->isSubscribed();

There was an existing infrequently-used method by the name every. I've renamed it to nth, to match lodash (not quite the same) and CSS :nth-child (almost the exact same).

@vlakoff
Copy link
Contributor

vlakoff commented Dec 13, 2016

I think the name is confusing. How about allPass for example?

@JosephSilber
Copy link
Member Author

This is the standard name used by JavaScript ES5, Underscore & Lodash.

@adamwathan
Copy link
Contributor

Makes me want to add an any alias for contains almost, fully support renaming the existing every method too.

@taylorotwell taylorotwell merged commit 77ee214 into laravel:master Dec 14, 2016
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

4 participants