php-standard-library/iter
Inspect and reduce any PHP iterable (arrays, generators, iterators) with small, focused helpers from PHP Standard Library - Iter. Designed for common iteration tasks and consistent behavior across iterable types.
Illuminate\Support\Collection provides rich functionality, iter offers lightweight, dependency-free alternatives for lazy operations (e.g., map/filter chains). This complements Laravel’s ecosystem without redundancy, especially for projects avoiding heavy frameworks like Symfony.Traversable objects) across Laravel’s layers—from API resources to Blade templates—reducing boilerplate and cognitive load. For example, transforming Eloquent results or API payloads becomes more declarative:
iter($posts)->map(fn($post) => new PostResource($post))->toArray();
@foreach, API resource transformations) while avoiding the overhead of Laravel Collections for simple iterable operations. This is particularly useful in microservices or data pipelines where composability is critical.spatie/array-to-object, league/glide). No conflicts with Laravel’s service container or dependency injection.iterable return types, named arguments) that Laravel 10+ supports natively, ensuring smooth integration with existing codebases.Pest, Mockery) for seamless adoption in CI/CD pipelines.artisan commands) and background jobs. Benchmarking against Laravel Collections will validate trade-offs (e.g., eager vs. lazy evaluation).foreach loops.map operations). Mitigated by:
iter()->tap() for debugging).Generator + ArrayObject) may need explicit type hints or runtime checks, adding minor complexity. Example:
if (!iter($mixedIterable)->isTraversable()) {
throw new InvalidArgumentException('Not traversable');
}
foreach loops. Offset by:
Collection::lazy() in Laravel 11+) that could reduce dependency on iter?iter() against Laravel Collections for critical operations (e.g., map/filter on 1M+ records). Metrics: memory usage, execution time.symfony/collection) that could conflict?assertEquals) handle iterable comparisons?Collection::map() with lazy iter()->map() for memory-intensive operations (e.g., API responses, report generation).
// Before
$transformed = collect($data)->map(fn($item) => $item * 2)->toArray();
// After
$transformed = iter($data)->map(fn($item) => $item * 2)->toArray();
Model::cursor() + iter():
iter(User::cursor())->filter(fn($user) => $user->active)->toArray();
iter($posts)->map(fn($post) => new PostResource($post))->toArray();
iter()->chunk(100) for chunked jobs).@foreach(iter($items)->filter(fn($item) => $item->visible) as $item)
{{ $item->name }}
@endforeach
spatie/array-to-object (for transforming iterables to objects) or league/glide (image processing pipelines).symfony/collection unless Symfony’s advanced features (e.g., IteratorAggregate) are explicitly needed.Pilot Phase (Low Risk):
foreach loops with iter() pipelines in new features.
Example: Replace a foreach in a make:report command with iter()->map().Incremental Adoption:
Collection::map() with iter()->map() for lazy operations in API controllers and services.iter() with Model::cursor() for large datasets (e.g., exports, audits).Resource classes for nested data transformations.Tooling Integration:
/** @return iter<array-key, mixed> */
function processItems(iterable $items): iter {
return iter($items)->map(...);
}
iter() as a global helper:
// app/Providers/AppServiceProvider.php
public function boot(): void {
app()->singleton('iter', fn() => new \Iter\Iter());
}
Deprecation Strategy:
iter() pipelines.foreach ($items as $item) with iter($items)->foreach(...)").Deprecates trait for custom iterators to warn developers.iterable type hints (e.g., @phpstan-ignore-next-line).match expressions).DB::select() generators).Model::cursor()).DB::table()->cursor()).iter($data)->cache('key', 60)->map(...); // Hypothetical extension
Storage facade for file iteration (e.g., Storage::disk()->files()).php-standard-library/iter to composer.json:
"require": {
"php-standard-library/iter": "^6.
How can I help you explore Laravel packages today?