zenstruck/collection
A lightweight PHP collection library by Zenstruck offering fluent, immutable-style operations for arrays and iterables. Provides map/filter/reduce, grouping, sorting, slicing, and helpful utilities to work with data sets cleanly in Laravel or standalone PHP.
Architecture Fit
The zenstruck/collection package provides a modern, Doctrine-aware collection utility that aligns well with Laravel’s existing Collection facade but extends functionality for complex data operations (e.g., batch processing, ORM integration, and pagination). Its support for PHP 8.5, Symfony 8, and DoctrineBundle 3 makes it a strategic choice for teams modernizing their stack. The package’s immutable collections, lazy evaluation, and Doctrine-specific optimizations (e.g., EntityResultQueryBuilder) reduce boilerplate for common operations like filtering, mapping, and batch processing. For Laravel projects, this package can replace or augment native collections where Doctrine interactions or advanced pagination (e.g., Pagerfanta 4+) are required.
Integration Feasibility
Collection methods (e.g., filter(), map()), reducing learning curves.EntityRepository, QueryBuilder, and Result objects, enabling ORM-aware operations without manual SQL.ORMServiceEntityRepository) are optional.Technical Risk
doctrine/orm and doctrine/dbal to v3+/v4+.EntityResult, QueryBuilder interactions).array<array-key, T> generics) enable stricter type safety in the package’s internals.Key Questions
ORMServiceEntityRepository), or are Laravel-native solutions sufficient?spatie/array, laravel/collections) conflict with zenstruck/collection?EntityRepository bridges) be tested in CI?EntityResult optimizations could mitigate?Stack Fit
Collection users, with zero configuration needed. Replace or extend native collections for:
DoctrineBridgeCollection for ORM-aware operations.BatchProcessor for chunked database operations.ORMServiceEntityRepository and EntityResult features align with Symfony’s ORM layer.Migration Path
Dependency Installation:
composer require zenstruck/collection:^0.8.0 --with-all-dependencies
composer.json to require PHP 8.5 and Doctrine ORM 3+ if using ORM features.Doctrine Integration (Optional):
Zenstruck\Collection\DoctrineBridge service (if provided in future versions) or manually bind repositories:
// Example: Configure EntityRepository services (Symfony)
services:
App\Entity\Repository\YourRepository:
decorates: 'doctrine.orm.entity_manager'
arguments: ['@.inner']
DoctrineBridgeCollection directly:
use Zenstruck\Collection\DoctrineBridgeCollection;
$collection = new DoctrineBridgeCollection(
$entityManager->getRepository(User::class)->findAll()
);
API Adoption:
Collection methods with package equivalents where beneficial:
// Before (Laravel)
$users = User::query()->get()->filter(fn ($user) => $user->isActive());
// After (zenstruck/collection)
$users = new DoctrineBridgeCollection(User::query()->get())
->filter(fn ($user) => $user->isActive());
$lazyUsers = new LazyCollection(
fn () => User::query()->cursor()
);
Pagination:
use Zenstruck\Collection\Pagerfanta\PagerfantaAdapter;
$adapter = new PagerfantaAdapter($collection);
$pagerfanta = new Pagerfanta($adapter);
Compatibility
Collection facade or service container.Sequencing
Phase 1: Dependency Update
composer.json to PHP 8.5 and install the package.filter(), map()) in isolation.Phase 2: Doctrine Integration (If Applicable)
DoctrineBridgeCollection and EntityResult features with critical queries.Phase 3: Feature Adoption
Phase 4: Performance Validation
spatie/array).QueryBuilder utilities.Maintenance
doctrine/orm and pagerfanta/pagerfanta for breaking changes, as they are core dependencies.Support
How can I help you explore Laravel packages today?