loophp/collection
A high-performance, functional-style collection library for PHP. Provides lazy, immutable, chainable operations built on generators to map, filter, reduce, group, zip, and more. Works standalone or with Laravel, aiming for speed, memory efficiency, and fluent pipelines.
Strengths:
map/filter chains), improving readability and testability in Laravel’s service layer.Fit Gaps:
Collection (e.g., no direct where/orderBy method chaining without adaptation).composer require loophp/collection; no core Laravel conflicts.Collection via aliases (e.g., use Loophp\Collection\Collection as LazyCollection).Collection methods (e.g., where → filter).Collection-dependent code may need adapters (e.g., Collection::where() → LazyCollection::filter()).foreach loops failing silently).Use Case Alignment:
Migration Strategy:
Collection?Collection::pluck()) be handled? (Adapter layer? Deprecation plan?)Performance:
Collection for critical paths (e.g., chunk(), paginate())?Tooling/DevEx:
cursor() for database streams)?Long-Term Viability:
Collection entirely, or is it better as a complement?artisan queue:work --once with large payloads).map over Kafka messages).Phase 1: Opt-In Adoption
app/Services/DataTransformer.php).use Loophp\Collection\Collection as LazyCollection;
class DataTransformer {
public function transform(array $rawData): LazyCollection {
return LazyCollection::from($rawData)
->filter(fn($item) => $item['active'])
->map(fn($item) => $item['name']);
}
}
use LazyCollection as LC).Phase 2: Hybrid Integration
Collection methods:
trait LaravelCollectionAdapter {
public function where(string $column, $operator, $value): self {
return $this->filter(fn($item) => $item[$column] $operator $value);
}
}
if (config('app.use_lazy_collections')) {
Collection::macro('lazy', fn() => LazyCollection::from($this));
}
Phase 3: Full Replacement (Optional)
Collection with Loophp\Collection via composer patch or custom framework fork.Laravel-Specific:
Collection::macro() (would need wrapper).Collection::make() (use LazyCollection::from()).Collection::pipe() (lazy collections are already pipable).Third-Party Packages:
spatie/array-to-object, laravel-excel may need updates.LazyCollection where needed.memory_get_usage() before/after).Undefined offset in lazy foreach loops).foreach → map).map calls).Collection usage.count() may trigger full iteration).push() → concat()).tap() for inspection).->get() at the end of a lazy chain (silent failures).How can I help you explore Laravel packages today?