Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Collections Laravel Package

illuminate/collections

Illuminate Collections provides a fluent, chainable API for working with arrays and iterables in PHP. Includes Collection and LazyCollection with powerful mapping, filtering, grouping, sorting, and higher-order operations, used widely across Laravel and standalone projects.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require illuminate/collections

This package provides the foundational Collection class used throughout Laravel ecosystems—often consumed indirectly via Laravel’s helpers like collect(). To use it directly, import the class:

use Illuminate\Support\Collection;

$collection = new Collection([1, 2, 3]);

The most immediate use case is transforming and querying arrays via fluent, chainable methods (e.g., map, filter, reduce, pluck). If you're already using Laravel, this package is already included—no need to install separately.

Implementation Patterns

  • Use collect() helper for convenience:
    $users = collect($rawUserArray)
        ->filter(fn($u) => $u['active'])
        ->map(fn($u) => $u['name'])
        ->values();
    
  • Leverage lazy collections (LazyCollection::make()) for memory-efficient processing of large data sets (e.g., iterating DB results without loading all records into memory).
  • Integrate with Eloquent: Queries return Collection instances; chain methods directly on results:
    $activeUsers = User::where('active', true)->get()
        ->sortByDesc('created_at')
        ->take(10);
    
  • Customize with macros: Extend Collection at runtime using Collection::macro(), e.g., for domain-specific logic:
    Collection::macro('snakeCaseKeys', function () {
        return $this->mapWithKeys(fn($item, $key) => [Str::snake($key) => $item]);
    });
    
  • Avoid intermediate arrays: Chain methods to prevent repeated iteration (e.g., prefer ->filter()->map()->values() over splitting into multiple statements).

Gotchas and Tips

  • Immutability: Collection is immutable for most methods—chains return new instances (e.g., filter() returns a new collection, original remains unchanged). This avoids side effects but may impact performance if used naively in tight loops.
  • Array access is supported but limited: While Collection implements ArrayAccess, prefer explicit methods like get(), first(), last() over array indexing ($collection[0]) for safety and readability.
  • Type safety caveats: PHPStan/Psalm may struggle with chained results unless you enable strictRules in your config or use PHPDoc for explicit type hints. Consider using @return Collection<int, string> in docblocks for clarity.
  • Memory over eager evaluation: LazyCollection is powerful but won’t help if you force eager evaluation (e.g., toArray(), all()). Only use lazy when you don’t need all results at once.
  • when() and tap() are debugging staples:
    $collection->tap(fn($c) => dump($c->count()))
        ->when($shouldFilter, fn($c) => $c->filter(...))
    
  • Macroable but not auto-loaded: Macros defined in service providers (e.g., in AppServiceProvider) only work if the provider is booted—remember to register them early.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests