PHPackages                             binary-cats/shard-collections - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. binary-cats/shard-collections

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

binary-cats/shard-collections
=============================

Enhance your Laravel collections with shard()

1.2.0(2mo ago)09[1 PRs](https://github.com/binary-cats/shard-collections/pulls)MITPHPPHP ^8.2CI passing

Since Mar 18Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/binary-cats/shard-collections)[ Packagist](https://packagist.org/packages/binary-cats/shard-collections)[ Docs](https://github.com/binary-cats/shard-collections)[ GitHub Sponsors]()[ RSS](/packages/binary-cats-shard-collections/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (22)Versions (8)Used By (0)

[![](https://camo.githubusercontent.com/f2ab9325e8f7af0213ff2a334875a5832098b04a7030cf73cb372065cb27d253/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f5368617264253230436f6c6c656374696f6e732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d62696e6172792d6361747325324673686172642d636f6c6c656374696f6e73267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d53686172642b796f75722b4c61726176656c2b636f6c6c656374696f6e732b696e746f2b6d756c7469706c652b7375622d636f6c6c656374696f6e732b62617365642b6f6e2b637573746f6d2b636f6e646974696f6e73266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d73756e)](https://camo.githubusercontent.com/f2ab9325e8f7af0213ff2a334875a5832098b04a7030cf73cb372065cb27d253/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f5368617264253230436f6c6c656374696f6e732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d62696e6172792d6361747325324673686172642d636f6c6c656374696f6e73267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d53686172642b796f75722b4c61726176656c2b636f6c6c656374696f6e732b696e746f2b6d756c7469706c652b7375622d636f6c6c656374696f6e732b62617365642b6f6e2b637573746f6d2b636f6e646974696f6e73266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d73756e)

Laravel Shard Collections
=========================

[](#laravel-shard-collections)

A Laravel package that provides powerful collection macros for sharding collections into multiple sub-collections based on custom conditions.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require binary-cats/shard-collections
```

Usage
-----

[](#usage)

This package provides two collection macros: `shard()` and `shardWithKeys()`. Both allow you to split a collection into multiple sub-collections based on custom conditions.

### The `shard()` Method

[](#the-shard-method)

The `shard()` method splits a collection into multiple sub-collections based on a series of conditions. Each condition is evaluated in order, and items that match a condition are moved to the corresponding sub-collection. Any remaining items are placed in the final sub-collection.

```
use Illuminate\Support\Collection;

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

$map = [
    fn ($item) => $item > 10,  // First condition
    fn ($item) => $item < 2,   // Second condition
];

$output = $collection->shard($map);

// Result:
// [
//     collect([]),           // Items > 10 (none)
//     collect([1]),          // Items < 2
//     collect([2, 3])        // Remaining items
// ]
```

You can also preserve the original keys by passing `true` as the second parameter:

```
$output = $collection->shard($map, true);

// Result:
// [
//     collect([]),           // Items > 10 (none)
//     collect([0 => 1]),     // Items < 2 (with original keys)
//     collect([1 => 2, 2 => 3]) // Remaining items (with original keys)
// ]
```

The remainder is included when it is not empty. You can force remainder by passing the last argument

```
$output = $collection->shard(
    map: $map,
    forceRemainder: true
);

// Result:
// [
//     collect(['one' => 1, 'two' => 2, 'three' => 3]), // Remaining items
//     collect([]) // Remainder is empty, but is included
// ]
```

### The `shardWithKeys()` Method

[](#the-shardwithkeys-method)

The `shardWithKeys()` method works similarly to `shard()` but allows you to specify custom keys for each sub-collection. This is particularly useful when you want to give meaningful names to your sharded collections.

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

$map = [
    'high' => fn ($item) => $item > 10,  // First condition
    'low' => fn ($item) => $item < 2,    // Second condition
];

$output = $collection->shardWithKeys($map, 'medium');

// Result:
// [
//     'high' => collect([]),    // Items > 10 (none)
//     'low' => collect([1]),     // Items < 2
//     'medium' => collect([2, 3]) // Remaining items
// ]
```

You can also preserve the original keys by passing `true` as the third parameter:

```
$output = $collection->shardWithKeys($map, 'medium', true);

// Result:
// [
//     'high' => collect([]),           // Items > 10 (none)
//     'low' => collect([0 => 1]),      // Items < 2 (with original keys)
//     'medium' => collect([1 => 2, 2 => 3]) // Remaining items (with original keys)
// ]
```

Working with Associative Collections
------------------------------------

[](#working-with-associative-collections)

Both methods work seamlessly with associative collections:

```
$collection = collect(['one' => 1, 'two' => 2, 'three' => 3]);

$map = [
    'small' => fn ($item) => $item < 2,
    'large' => fn ($item) => $item > 10,
];

$output = $collection->shardWithKeys($map, 'medium', true);

// Result:
// [
//     'small' => collect(['one' => 1]),
//     'large' => collect([]),
//     'medium' => collect(['two' => 2, 'three' => 3])
// ]
```

The remainder is included when it is not empty. You can force remainder by passing the last argument

```
collect(['one' => 1, 'two' => 2, 'three' => 3])->shard(
    map: [fn ($item) => $item < 10],
    forceRemainder: true
);

// Result:
// [
//     collect(['one' => 1, 'two' => 2, 'three' => 3]) // Remaining items
//     collect([]) // Remainder is empty, but is included
// ]
```

Empty Maps
----------

[](#empty-maps)

When you provide an empty map, both methods will return a single collection containing all items:

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

$output = $collection->shard([]);
// Result: [collect([1, 2, 3])]

$output = $collection->shardWithKeys([], 'all');
// Result: ['all' => collect([1, 2, 3])]
```

Testing
-------

[](#testing)

```
composer test
```

Support us
----------

[](#support-us)

[Binary Cats](https://binarycats.dev) is a webdesign agency based in Illinois, US.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance89

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~122 days

Total

4

Last Release

60d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ebb48367388b4368b14cca42714bb13002d7414d9dc8da19c5490ef65c059719?d=identicon)[cyrill.kalita@gmail.com](/maintainers/cyrill.kalita@gmail.com)

---

Top Contributors

[![cyrillkalita](https://avatars.githubusercontent.com/u/2401848?v=4)](https://github.com/cyrillkalita "cyrillkalita (20 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

laravelcollectionbinary-catsshard

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/binary-cats-shard-collections/health.svg)

```
[![Health](https://phpackages.com/badges/binary-cats-shard-collections/health.svg)](https://phpackages.com/packages/binary-cats-shard-collections)
```

###  Alternatives

[lorisleiva/lody

Load files and classes as lazy collections in Laravel.

956.6M9](/packages/lorisleiva-lody)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[ziming/laravel-scrapingbee

A PHP Laravel Library for ScrapingBee

4310.6k](/packages/ziming-laravel-scrapingbee)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
