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

wp-starter/collections

Laravel-friendly collection utilities for WordPress projects. Adds helpful helpers and abstractions around arrays and iterable data to make transforming, filtering, and mapping data easier in WP-driven apps, plugins, and themes with a modern PHP approach.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require wp-starter/collections
    

    Ensure autoload is dumped:

    composer dump-autoload
    
  2. Basic Usage Import the core class:

    use WpStarter\Collections\Collection;
    

    Initialize with an array:

    $collection = new Collection([1, 2, 3]);
    
  3. First Use Case: Filtering Posts

    $posts = new Collection($wpdb->get_results("SELECT * FROM posts"));
    $published = $posts->where('post_status', 'publish');
    

Implementation Patterns

Common Workflows

  1. Chaining Methods

    $filtered = $collection
        ->where('status', 'active')
        ->sortBy('created_at')
        ->take(10);
    
  2. Integration with Eloquent

    $users = User::all()->toBase()->pipe(function ($users) {
        return new Collection($users);
    });
    
  3. Custom Collections

    class PostCollection extends Collection {
        public function published() {
            return $this->where('post_status', 'publish');
        }
    }
    

Laravel-Specific Patterns

  • Service Provider Binding

    $this->app->bind(Collection::class, function () {
        return new Collection();
    });
    
  • Macro Extensions

    Collection::macro('activeOnly', function () {
        return $this->where('is_active', true);
    });
    
  • API Response Formatting

    return response()->json($collection->map(fn ($item) => $item->toArray()));
    

Gotchas and Tips

Pitfalls

  1. Immutable Operations Methods like where(), sortBy(), etc., return new instances. Use -> chaining or reassign:

    $filtered = $collection->where(...); // Correct
    $collection->where(...); // Silent failure (original unchanged)
    
  2. Array vs. Object Handling Collections assume array-like access. For objects, ensure get()/set() methods exist or use ->toArray() first.

  3. Performance with Large Datasets Avoid eager-loading entire tables into a Collection. Use database cursors or chunking:

    $collection = new Collection();
    $wpdb->get_results("SELECT * FROM large_table", ARRAY_A, $collection);
    

Debugging Tips

  • Inspect Internals

    $collection->dump(); // Dumps the underlying array
    
  • Check for Mutations Use tap() to debug intermediate steps:

    $collection->tap(fn ($c) => Log::debug($c->all()));
    

Extension Points

  1. Custom Comparators

    Collection::macro('customSort', function () {
        return $this->sortBy(function ($item) {
            return $item->customProperty;
        });
    });
    
  2. Override Default Behavior Extend the class and modify get(), set(), or offsetGet() methods.

  3. Leverage Laravel’s Collection Synergy Use merge(), concat(), or crossJoin() for advanced operations:

    $merged = $collection->merge($anotherCollection);
    

Configuration Quirks

  • No Built-in Config The package is lightweight; no config/collections.php exists. All logic is method-based.

  • Type Safety No native type hints (e.g., array|object). Validate inputs manually if needed:

    if (!is_array($data)) {
        throw new \InvalidArgumentException('Expected array');
    }
    
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.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php