Laravel Magazine
Collection partition() and sole(): Two Methods That Replace Ugly Loops

Collection partition() and sole(): Two Methods That Replace Ugly Loops

Eric Van Johnson ·

Laravel's Collection class has well over a hundred methods, and it's easy to only ever reach for filter, map, and each. Two lesser-used methods, partition() and sole(), each replace a specific loop pattern that shows up constantly in real applications.

partition(): splitting into two groups in one pass

The common case: you need both the items that match a condition and the items that don't. The naive approach filters twice:

$paid = $invoices->filter(fn ($invoice) => $invoice->isPaid());
$unpaid = $invoices->reject(fn ($invoice) => $invoice->isPaid());

That's two full passes over the collection, evaluating the same condition twice. partition() does it in one:

[$paid, $unpaid] = $invoices->partition(fn ($invoice) => $invoice->isPaid());

$paid and $unpaid are both Collection instances, ready to count(), sum(), or render separately in a view — useful for exactly the kind of "here's the matching group and here's everything else" display that dashboards need constantly (paid vs. unpaid, active vs. inactive, passed vs. failed).

sole(): "there should be exactly one, give it to me"

This one addresses a subtly different problem than first(). first() silently returns whatever the first match is — one result, five results, doesn't matter, you get one back either way. Sometimes that's not what your logic actually means. If you're looking up a user's single active subscription, "more than one active subscription" isn't a case you want silently ignored — it's a bug you want to know about immediately.

$activeSubscription = $subscriptions->sole(fn ($sub) => $sub->status === 'active');

sole() throws Illuminate\Support\ItemNotFoundException if nothing matches, and Illuminate\Support\MultipleItemsFoundException if more than one item matches. Compare that to first(), which returns null on no match and silently discards extra matches — both of which can hide a real data integrity problem behind code that appears to work fine in testing.

// first() hides the bug: if two subscriptions are somehow active,
// you silently get one of them and never find out.
$activeSubscription = $subscriptions->first(fn ($sub) => $sub->status === 'active');

// sole() surfaces it immediately as an exception.
$activeSubscription = $subscriptions->sole(fn ($sub) => $sub->status === 'active');

Where sole() earns its keep

Use it anywhere your data model has an implicit "there can be only one" rule that isn't (or can't easily be) enforced by a database constraint — a single primary contact per account, a single default payment method, a single in-progress import job per tenant. sole() turns a violated assumption into a loud exception at the point where it matters, instead of a quiet first() call that returns the wrong record and produces a confusing bug three layers away from the actual cause.

Neither method is exotic — both have been in the Collection class for years — but they replace two of the most common hand-rolled loop patterns in everyday Laravel code, and the built-in versions are both faster and clearer about intent than the loop they replace.

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.