werxe/laravel-collection-macros
A handy set of macros that extends Laravel Collections with extra methods for common array and data transformations, letting you write cleaner, more expressive pipelines without custom helper functions or repetitive loops.
composer require werxe/laravel-collection-macros. v7 requires Laravel 9.0+; if you're still on Laravel 8 you'll silently resolve v6.x — verify with composer show werxe/laravel-collection-macros after install.Illuminate\Support\Collection at boot. Once installed, the helpers are available everywhere — including on Eloquent collections (User::all()->...), which is where most of the day-to-day value shows up.src/ tree up front.sortBy closures or ->groupBy()->map() gymnastics, you write $orders->sortByMany(['status' => 'asc', 'created_at' => 'desc']) and move on. The second common entry point is transpose/rotate when turning result rows into columns for a pivot table or CSV.$rows->filterMap(fn ($r) => ...)->transpose()->map(...). Prefer this over extracting intermediate variables.->pluck('name', 'id')->toArray() into one call are the highest-frequency win in controllers and API resources.ifAny / ifEmpty keep fallback logic inside the chain instead of forcing an if ($c->isNotEmpty()) block around it.->map(); this is much cleaner than each() with an external accumulator.Collection::macro() calls in a dedicated CollectionMacrosServiceProvider (or AppServiceProvider::boot()), not scattered across files — so vendor helpers and in-house helpers are discoverable in one place and boot order is predictable.(new Collection([...]))->macroName() works fine in unit tests; only call Collection::flushMacros() in tearDown if you genuinely need to assert on absence — it wipes other packages' macros too.^7.0 and treat major releases here as "check the Laravel requirement" releases.Collection class, so collisions are the number-one failure mode. Laravel keeps absorbing good collection helpers natively — a name that was unique in Laravel 8 may be a first-class method in 9/10/11, in which case the native method wins and your macro appears to do nothing.Collection::hasMacro('name') and grep composer.json for other collection-macro packages.$this shadowing. Inside a macro closure $this is the collection instance, not your controller or service. Capture outer state explicitly with use ($var).@method docblocks on a stub class or use laravel-ide-helper, or every call site is a squiggle with no jump-to-definition.config:cache/route:cache don't affect macro registration, but a deferred provider that registers macros lazily will bite you if anything calls the macro before the provider resolves.Collection::hasMacro($name) plus a ReflectionFunction on Collection::getMacro($name) tells you whether you're hitting the package or an app override — faster than reading stack traces.Collection::macro('mySortMany', fn (...) => $this->sortByMany(...))) so app-specific rules stay in your codebase and survive package upgrades.How can I help you explore Laravel packages today?