Laravel collection filter by condition and split

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Laravel Collections: Splitting Data Based on Conditions Efficiently

When working with data in Laravel, you frequently encounter scenarios where you need to segment a single collection into multiple subsets based on specific criteria. A common requirement is separating items marked as "featured" from those that are not.

Let's explore how we can efficiently achieve this separation using the powerful methods available in the Laravel Collections facade.

The Baseline Approach: Simple Filtering

The most straightforward way to solve this problem is by applying two separate filter() calls, exactly as you initially suggested. This method is highly readable and explicitly states the intent of the code.

$collection = collect([
    ['name' => 'Item A', 'featured' => true],
    ['name' => 'Item B', 'featured' => false],
    ['name' => 'Item C', 'featured' => true],
]);

$featured = $collection->filter(function ($item) {
    return $item['featured'];
});

$unfeatured = $collection->filter(function ($item) {
    return !$item['featured'];
});

/*
$featured will be: [ [ 'name' => 'Item A', 'featured' => true ], [ 'name' => 'Item C', 'featured' => true ] ]
$unfeatured will be: [ [ 'name' => 'Item B', 'featured' => false ] ]
*/

This approach is perfectly correct and easy to debug. However, as we often deal with performance and code brevity in large applications, developers always seek more concise solutions.

The Shorter Way: Leveraging groupBy for Dynamic Separation

While two separate filters work, there is a more idiomatic and potentially more performant way to handle this kind of dynamic separation using grouping methods. Instead of iterating over the collection twice, we can iterate once or use specialized grouping functions to construct our desired subsets.

A highly effective pattern involves using groupBy() based on the boolean value. This allows you to group all items into two distinct buckets immediately.

$collection = collect([
    ['name' => 'Item A', 'featured' => true],
    ['name' => 'Item B', 'featured' => false],
    ['name' => 'Item C', 'featured' => true],
]);

// Group the collection based on the boolean value of the 'featured' attribute.
$grouped = $collection->groupBy('featured');

// Extract the two required collections from the grouped results.
$featured = $grouped->get(true);
$unfeatured = $grouped->get(false);

/*
$featured will be: [ [ 'name' => 'Item A', 'featured' => true ], [ 'name' => 'Item C', 'featured' => true ] ]
$unfeatured will be: [ [ 'name' => 'Item B', 'featured' => false ] ]
*/

Why groupBy is Superior

The advantage of using groupBy() here lies in efficiency and data integrity. Instead of iterating over the entire collection twice (once for $featured and once for $unfeatured), you iterate over it only once when performing the grouping operation. If your collection were massive, this single pass can offer better performance characteristics, aligning with the principles of optimized code architecture found in frameworks like Laravel.

Furthermore, groupBy() provides a structured result—an array where each key corresponds to the group value (true or false), making it very easy to access the specific subsets you need using the get() method. This is a powerful pattern for complex data segmentation within any collection operation.

Conclusion: Choosing the Right Tool

Both methods achieve the desired result, but they serve different purposes:

  1. Two Separate filter() calls: Best for simple filtering where you need two completely independent lists and maximum readability is paramount.
  2. groupBy(): Best for dynamic segmentation where you intend to create multiple distinct buckets from a single pass. It often leads to cleaner, more performant code when dealing with complex grouping scenarios.

As senior developers, we aim not just for correctness, but for elegant, efficient solutions. For splitting collections based on boolean conditions in Laravel, mastering methods like groupBy() allows you to write code that is both highly readable and optimized. Remember, leveraging the built-in power of the Collection facade is key to building robust applications, much like adhering to best practices promoted by the wider Laravel community on sites like https://laravelcompany.com.