Skip to content

[12.x] Add hasSole() method to collections#58463

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
JosephSilber:has-sole
Jan 23, 2026
Merged

[12.x] Add hasSole() method to collections#58463
taylorotwell merged 1 commit intolaravel:12.xfrom
JosephSilber:has-sole

Conversation

@JosephSilber
Copy link
Contributor

This PR renames containsOneItem() to hasSole(), and adds callback and operator support.

Background

Back in #36428, I added an isSingle() method to check if a collection contains exactly one item. Taylor later renamed it to containsOneItem() because—and I quote—"it's not a dating app" 😄

The problem is that containsOneItem() is actually longer than count() === 1, making the method rather pointless.

New Method

Now that we have the sole() nomenclature, we can add a hasSole() method that...

  • Is shorter and more expressive than containsOneItem()
  • Supports the same filter signatures as sole() (callback, key/value, and full operator syntax)
$collection->hasSole();
$collection->hasSole(fn ($item) => $item->active);
$collection->hasSole('status', 'pending');
$collection->hasSole('age', '>=', 21);

Deprecation

containsOneItem() is now deprecated and delegates to hasSole().

Laravel 13

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

@JosephSilber JosephSilber force-pushed the has-sole branch 3 times, most recently from 8ec5fbc to ea0ab6f Compare January 22, 2026 22:39
@shaedrich
Copy link
Contributor

Back in #36428, I added an isSingle() method to check if a collection contains exactly one item. Taylor later renamed it to containsOneItem() because—and I quote—"it's not a dating app" 😄

We were so close to getting isPromiscuous() instead of containsManyItems() 😲

@taylorotwell taylorotwell merged commit 451016e into laravel:12.x Jan 23, 2026
68 of 70 checks passed
@mchev
Copy link

mchev commented Jan 29, 2026

And a handy mnemonic to remember this one

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