Installation Add the package via Composer:
composer require hiqdev/php-collection
No additional configuration is required—it’s a drop-in library.
First Use Case
Replace Laravel’s native Collection with HiQDev\Collection\Collection for enhanced functionality:
use HiQDev\Collection\Collection;
$items = new Collection([1, 2, 3, 4, 5]);
$evenSquares = $items->filter(fn($n) => $n % 2 === 0)
->map(fn($n) => $n ** 2)
->all();
// Output: [4, 16]
Where to Look First
vendor/hiqdev/php-collection/src/).HiQDev\Collection\Collection and its methods (e.g., groupBy, diff, except).Collection facade in config/app.php (if needed):
'aliases' => [
'Collection' => HiQDev\Collection\Collection::class,
],
Data Transformation
Use map, filter, and reduce for declarative operations:
$users = new Collection([
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25],
]);
$names = $users->pluck('name')->implode(', '); // "Alice, Bob"
Grouping and Aggregation
Leverage groupBy and countBy for analytics:
$orders = new Collection([
['product' => 'Laptop', 'status' => 'shipped'],
['product' => 'Mouse', 'status' => 'pending'],
]);
$grouped = $orders->groupBy('status');
// ['shipped' => [...], 'pending' => [...]]
Set Operations
Use diff, intersect, or merge for array comparisons:
$activeUsers = new Collection([1, 2, 3]);
$inactiveUsers = new Collection([3, 4, 5]);
$uniqueActive = $activeUsers->diff($inactiveUsers); // [1, 2]
Lazy Evaluation
Chain methods with lazy() for performance-critical pipelines:
$largeData = new Collection(range(1, 1_000_000));
$result = $largeData->lazy()
->filter(fn($n) => $n % 7 === 0)
->take(10)
->toArray();
$this->app->bind(
Illuminate\Support\Collection::class,
HiQDev\Collection\Collection::class
);
Collection::macro('snakeCaseKeys', function () {
return $this->mapWithKeys(fn($item) => [
Str::snake(key($item)) => $item,
]);
});
Backward Compatibility
pluck) may behave differently than Laravel’s Collection. Test edge cases:
$collection = new Collection([['id' => 1], ['id' => 2]]);
$collection->pluck('id'); // Returns [1, 2] (vs. Laravel's object-like access).
->all() or ->toArray() if expecting arrays.Performance Overhead
// Avoid:
$collection->filter(...)->map(...)->count();
// Prefer:
array_filter($array, ...);
Missing Laravel Features
when, tap, or unless methods. Reimplement or use native PHP:
$collection->when($condition, fn($coll) => $coll->filter(...));
getMethods() to list available methods:
print_r(get_class_methods(HiQDev\Collection\Collection::class));
->dump() or ->toArray() to inspect intermediate states:
$result = $collection->lazy()->filter(...)->dump()->take(5);
Custom Iterators
Implement IteratorAggregate for bespoke traversal:
class CustomCollection extends Collection implements IteratorAggregate {
public function getIterator() {
return new CustomIterator($this->items);
}
}
Type-Safe Collections
Use PHP 8’s array_object or generics (via mypy or phpstan) to enforce types:
/** @var Collection<int, User> */
$users = new Collection([], [], User::class);
Serialization
Override jsonSerialize() for custom output:
Collection::macro('jsonSerialize', function () {
return $this->map(fn($item) => (array) $item)->toArray();
});
config/hiqdev-collection.php exists.use Collection as HiQCollection;
How can I help you explore Laravel packages today?