Skip to content

Implement hasAll(), hasAny(), doesNotHave() and doesNotHaveAny() #26543

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

Closed
wants to merge 1 commit into from
Closed

Implement hasAll(), hasAny(), doesNotHave() and doesNotHaveAny() #26543

wants to merge 1 commit into from

Conversation

troccoli
Copy link
Contributor

@troccoli troccoli commented Nov 16, 2018

Many time I had to check whether an item did not exist in a collection, and using the ! operator always seemed a bit less readable that it could be. So, I decided to implement the doesNotHave() method.

I have also implemented other useful, IMHO, methods to help: hasAny() and doesNotHaveAny().

The current has() method as been deprecated in favour of hasAll() which is more in line to what it actually does.

@derekmd
Copy link
Contributor

derekmd commented Nov 16, 2018

doesNotHave = collect($items)->only($key)->isEmpty()
doesNotHaveAny = collect($items)->only($keys)->isEmpty()
hasAny = collect($items)->only($keys)->isNotEmpty()

@troccoli
Copy link
Contributor Author

troccoli commented Nov 16, 2018

I don't know you, but I'd rather write, and read,

if ($collection->hasAny("first", "second"))

than

if ($collection->only("first", "second")->isNotEmpty())

@taylorotwell
Copy link
Member

No plan on adding at the moment.

@devajmeireles
Copy link
Contributor

@taylorotwell Do you agree if I send hasAll to the collection? I think it can be very useful now in daily tasks.

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.

4 participants