Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Dividing Collections with Partition and Split in Laravel

A Laravel collection holds an ordered sequence of items and gives you methods for working with them. When you call partition(), Laravel tests each item against a condition and places it into one of two groups. When you call split(), Laravel divides the sequence into the number of groups you ask for. Both methods return an outer collection that holds those groups as smaller collections.

The examples below follow the Laravel 13.x collection documentation.

Use partition() to separate items by a condition

When you pass a callback to partition(), Laravel checks each item against your condition. It puts items that pass into the first collection and items that fail into the second. This example separates values below three from the rest:

$collection = collect([1, 2, 3, 4, 5, 6]);

[$underThree, $threeOrMore] = $collection->partition(
    fn (int $value) => $value all(): [1, 2]
// $threeOrMore->all(): [3, 4, 5, 6]

The method returns an outer collection containing two inner collections. PHP destructuring assigns each inner collection to a variable by position, so you get named variables that later code can use to handle each group on its own. Laravel documents this behavior in its collection reference.

Use partition() when you need both sides of a yes-or-no decision. A payment workflow might separate paid orders from unpaid ones, or a validation step might separate valid records from invalid ones. If you need to sort records into more than two categories, partition() cannot express that split, so call a method like groupBy() instead.

Use split() to create a requested number of groups

Pass split() the number of groups you want. Laravel divides the collection into that many groups while keeping the items in their original order, as in the documentation's example:

$collection = collect([1, 2, 3, 4, 5]);

$groups = $collection->split(3);

// $groups->all(): [[1, 2], [3, 4], [5]]

Here, the outer collection holds three inner collections. When you call all(), Laravel returns the nested values as arrays, which makes the output shape easy to inspect. Call split() when you want to distribute a sequence across a fixed number of display columns or processing groups. If you want each group to hold a fixed number of items instead of targeting a total group count, use Laravel's chunk() and chunkWhile() methods.

Choose splitIn() when earlier groups should fill first

Laravel also includes splitIn(). It divides items into the number of groups you request by filling earlier groups before placing the remaining items in the last group. The documented example produces this result:

collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    ->splitIn(3)
    ->all();

// [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]

In practice, you call partition() to separate items with a condition, split() to divide a sequence into a set number of groups, and splitIn() when you want Laravel to fill the earlier groups before putting the leftovers in the last one. The Laravel collection documentation shows how split() and splitIn() distribute items differently.

Eloquent collections return a distinct outer type for partition()

When you call partition() on an Eloquent collection, Laravel returns an outer Illuminate\Support\Collection that holds two Illuminate\Database\Eloquent\Collection instances. That distinction matters if your later code checks the outer collection's class or calls Eloquent-only methods on it instead of on the inner groups. You can check the exact return shape in Laravel's Eloquent collection documentation.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *