Skip to content

[12.x] Add hasMany() method to collections#58550

Merged
taylorotwell merged 2 commits intolaravel:12.xfrom
JosephSilber:collections-has-many
Jan 30, 2026
Merged

[12.x] Add hasMany() method to collections#58550
taylorotwell merged 2 commits intolaravel:12.xfrom
JosephSilber:collections-has-many

Conversation

@JosephSilber
Copy link
Contributor

This PR renames containsManyItems() to hasMany(), and adds callback and operator support.

Background

In #58312, containsManyItems() was added as a companion to containsOneItem(). Now that we have hasSole() (#58463), we should add a matching hasMany() method.

This is now more coherent:

$collection->isEmpty();
$collection->hasSole();
$collection->hasMany();

New Method

hasMany() checks if a collection contains more than one item, with the same filter signatures as hasSole():

$collection->hasMany();
$collection->hasMany(fn ($item) => $item->active);
$collection->hasMany('status', 'pending');
$collection->hasMany('age', '>=', 21);
$collection->hasMany->active;

Improvements

containsManyItems() had several issues:

  • Overly complex implementation: The method in Collection used a manual complex loop, instead of simply using take(2)->count().
  • Inconsistent signatures: Collection accepted a callback, LazyCollection did not.
  • No higher-order proxy support: Not included in the $proxies array.
  • Missing lazy tests: LazyCollection::containsManyItems() had no laziness tests.

hasMany() addresses all of these with a single, shared implementation in the EnumeratesValues trait.

Deprecation

containsManyItems() is now deprecated and delegates to hasMany().

Laravel 13

After this is merged, and then merged into master, we'll add this method to the Enumerable interface, and remove the containsManyItems() method completely.

@taylorotwell taylorotwell merged commit 635a29f into laravel:12.x Jan 30, 2026
70 checks passed
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

Comments