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.
map, filter, reduce, etc.) integrate seamlessly with Laravel’s service containers, dependency injection, and method injection patterns. For example, a Laravel service can accept an iterable and process it lazily:
public function processData(iterable $data): array {
return iter($data)->map(fn($item) => $this->transform($item))->toArray();
}
artisan commands) and queue workers, where generators (e.g., Model::cursor()) can be processed without materializing entire datasets in memory.iterable return types, named arguments) that Laravel 10+ supports, ensuring smooth adoption.@method iterable map(callable $callback)).tap() for debugging intermediate states (e.g., iter()->tap(fn($i) => Log::debug($i))).iter()->map(fn($x) => $x->invalidMethod())).Generator + ArrayObject) may require explicit type handling. Address via:
instanceof or is_iterable() guards in critical paths.iter($data)->map()->filter()).iter()->map()) against eager collections (Collection::map()) for critical operations (e.g., API responses, batch jobs).foreach loops, custom iterators) that this package could resolve.app('iter'))?iter() pipelines.Model::all() with Model::cursor() + iter() for lazy loading in exports, audits, or bulk operations.
iter(User::cursor())->filter(fn($user) => $user->isActive())->each(fn($user) => $user->sendWelcomeEmail());
Resource classes.
public function toArray($request): array {
return iter($this->posts)->map(fn($post) => ['title' => $post->title, 'author' => $post->user->name])->toArray();
}
iter($users)->chunk(100)->each(fn($chunk) => UserJob::dispatch($chunk));
@foreach(iter($posts)->filter(fn($post) => $post->isPublished) as $post)
{{ $post->title }}
@endforeach
iter(Storage::files('logs'))->filter(fn($file) => Str::contains($file, 'error'))->each(fn($file) => Log::error($file));
spatie/array-to-object or spatie/laravel-medialibrary for lazy file/image processing.league/glide for image processing pipelines (e.g., iter($images)->map(fn($img) => Glide::image($img)->resize(...))).IteratorAggregate features (e.g., LimitIterator) are explicitly needed.iter() for lazy operations where Collection is eager or memory-intensive.iter() for lightweight needs; use Symfony for advanced features (e.g., LimitIterator, CachingIterator).Pilot Phase (2–4 Weeks):
foreach loops with iter() pipelines in a single module.// Before
$activeUsers = [];
foreach (User::all() as $user) {
if ($user->isActive) {
$activeUsers[] = $user->name;
}
}
// After
$activeUsers = iter(User::all())->filter(fn($user) => $user->isActive)->pluck('name')->toArray();
Core Integration (4–8 Weeks):
iter() as a global helper (e.g., app('iter')).// Laravel Service Provider
$this->app->singleton('iter', fn() => new \Iter\Iter());
// Usage in Controllers
$data = app('iter')->map($rawData, fn($item) => $this->transform($item));
Full Adoption (8–12 Weeks):
How can I help you explore Laravel packages today?