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

[6.x] Add whereNull and whereNotNull to Collection #31425

Merged
merged 5 commits into from
Feb 11, 2020
Merged

[6.x] Add whereNull and whereNotNull to Collection #31425

merged 5 commits into from
Feb 11, 2020

Conversation

SjorsO
Copy link
Contributor

@SjorsO SjorsO commented Feb 11, 2020

This PR adds whereNull and whereNotNull methods to the Collection class.

These methods are already available when building a query:

$users = User::whereNotNull('email_verified_at')->get();

But as soon as you have a collection, you need to do this instead:

$users = User::all();

$unverifiedUsers = $users->whereStrict('is_verified_at', null);

$verifiedUsers = $users->where('is_verified_at', '!==', null);

With this PR, you can do this instead:

$users = User::all();

$unverifiedUsers = $users->whereNull('is_verified_at');

$verifiedUsers = $users->whereNotNull('is_verified_at');

@taylorotwell
Copy link
Member

What if you have a collection of only values? collect([1, null, 3])

@GrahamCampbell GrahamCampbell changed the title [6.x] add whereNull and whereNotNull to Collection [6.x] Add whereNull and whereNotNull to Collection Feb 11, 2020
@SjorsO
Copy link
Contributor Author

SjorsO commented Feb 11, 2020

@taylorotwell

The documentation doesn't mention anything about using ->where() on a collection of simple values, so I'm not sure if this is a good idea, but I just pushed a commit that adds support for whereNull and whereNotNull for collections using ->where(null, '!==', null). I'm not sure if doing it this way has any unintended side-effects.

It might be a better idea to not support using these methods on a collection of simple values, or to do something like this instead:

if (func_num_args() === 0) {
    // do a normal filter
}

return $this->where($key, '!==', null);

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