Download the PHP package binary-cats/shard-collections without Composer
On this page you can find all versions of the php package binary-cats/shard-collections. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download binary-cats/shard-collections
More information about binary-cats/shard-collections
Files in binary-cats/shard-collections
Package shard-collections
Short Description Enhance your Laravel collections with shard()
License MIT
Homepage https://github.com/binary-cats/shard-collections
Informations about the package 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
You can install the package via composer:
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 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.
You can also preserve the original keys by passing true
as the second parameter:
The remainder is included when it is not empty. You can force remainder by passing the last argument
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.
You can also preserve the original keys by passing true
as the third parameter:
Working with Associative Collections
Both methods work seamlessly with associative collections:
The remainder is included when it is not empty. You can force remainder by passing the last argument
Empty Maps
When you provide an empty map, both methods will return a single collection containing all items:
Testing
License
The MIT License (MIT). Please see License File for more information.