PHPackages                             loilo/collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. loilo/collection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

loilo/collection
================

An extended version of Laravel's collections

1.0.0(7y ago)116MITPHPPHP ^7.1CI failing

Since Oct 29Pushed 5y ago1 watchersCompare

[ Source](https://github.com/loilo/Collection)[ Packagist](https://packagist.org/packages/loilo/collection)[ RSS](/packages/loilo-collection/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Collection
==========

[](#collection)

[![Travis](https://camo.githubusercontent.com/3868b137565e63b738171e2133bf019d8b9ddc636fe5be911ff09746a53b29f5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4c6f696c6f2f436f6c6c656374696f6e2e737667)](https://travis-ci.org/Loilo/Collection) [![Packagist](https://camo.githubusercontent.com/e1a36b847277b70024dd1d9ed61f144a79745116732c1d72020ce42c0ca8aa05/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f696c6f2f636f6c6c656374696f6e2e737667)](https://packagist.org/packages/loilo/collection)

> An extended version of Laravel's collections

This is my very personal extension of Laravel's amazing [Collection](https://laravel.com/docs/collections) class (as extracted by [tightenco/collect](https://github.com/tightenco/collect)). It adds some methods that I need regularly.

The declared aim of this project is to feed its methods back into Laravel by making proposals in [`laravel/ideas`](https://github.com/laravel/ideas).

Install
-------

[](#install)

```
composer require loilo/collection
```

Methods
-------

[](#methods)

> **Note:** In all code examples below, `Collection` refers to `Loilo\Collection\Collection`.

**Table of Contents:**

- [`insertBefore()`/`insertAfter()`](#insertbeforeinsertafter)
- [`mergeInBefore()`/`mergeInAfter()`](#mergeinbeforemergeinafter)
- [`extract()`](#extract)
- [`rearrange()`](#rearrange)

### `insertBefore()`/`insertAfter()`

[](#insertbeforeinsertafter)

Inserts items before/after a reference item (modifies the collection).

**Proposal:** [\#650](https://github.com/laravel/ideas/issues/650) (declined)

**Details:** [Specification](spec/InsertBeforeAfter.md)

**Examples:**

Insert a list of items after a certain value:

```
$c = Collection::make([ 10, 20, 30, 40 ]);
$c->insertAfter(20, [ 24, 25, 26 ]);
$c->all() === [ 10, 20, 24, 25, 26, 30, 40 ];
```

Insert data after a certain key:

```
$c = Collection::make([
    'name' => 'loilo/collection',
    'version' => '1.0.0'
]);

$c->insertAfter(function ($value, $key) {
    return $key === 'name';
}, [ 'description' => "An extended version of Laravel's collections" ]);

$c->all() === [
    'name' => 'loilo/collection',
    'description' => "An extended version of Laravel's collections",
    'version' => '1.0.0'
];
```

### `mergeInBefore()`/`mergeInAfter()`

[](#mergeinbeforemergeinafter)

These are equivalent to `insertBefore()`/`insertAfter()` but do not modify the collection.

### `extract()`

[](#extract)

Extract structures from the collection, kind of an advanced [`pluck()`](https://laravel.com/docs/collections#method-pluck).

**Details:** [Specification](spec/Extract.md)

**Examples:**

Take certain items from a list:

```
$c = Collection::make([ 1, 2, 3 ])->extract([ 0, 1 ])
$c->all() === [ 1, 2 ];
```

Transform a collection to a certain structure:

```
$c = Collection::make([
    'a' => [
        'd' => 3,
        'e' => 4
    ],
    'b' => [
        'd' => 5,
        'e' => 6
    ],
    'c' => [
        'd' => 7,
        'e' => 8
    ]
]);

$c->extract([ 'a' ])->all() === [
    'a' => [
        'd' => 3,
        'e' => 4
    ]
];

$c->extract([ '*' => [ 'd' ] ])->all() === [
    'a' => [ 'd' => 3 ],
    'b' => [ 'd' => 5 ],
    'c' => [ 'd' => 7 ]
];
```

### `rearrange()`

[](#rearrange)

This method re-orders a collection, according to a predefined order.

```
Collection::make([ 'a', 'b', 'c' ]);
    ->rearrange([ 2, 0, 1 ])
    ->all() === [ 'c', 'a', 'b' ];
```

#### Behavior of Unarrangeable Items

[](#behavior-of-unarrangeable-items)

When a collection item is not matched by the new order, it will be appended to the ordered items:

```
Collection::make([ 'a', 'b', 'c' ]);
    ->rearrange([ 1 ])
    ->all() === [ 'b', 'a', 'c' ];
```

This behavior is controlled by the second argument of the `rearrange()` method. The default behavior `$c->rearrange([ ... ])` is equivalent to `$c->rearrange([ ... ], Collection::UNARRANGEABLE_APPEND)`.

Possible values for this parameter are:

Class ConstantDescription`UNARRANGEABLE_APPEND`Appends unarrangeable items after the rearranged ones.`UNARRANGEABLE_PREPEND`Prepends unarrangeable items before the rearranged ones.`UNARRANGEABLE_PARTITION`Partitions the return value and maps rearranged and unarrangeable items to the `rearranged` respectively the `unarrangeable` key.`UNARRANGEABLE_DISCARD`Omits the unarrangeable items from the returned collection.`UNARRANGEABLE_THROW`Throws an `UnexpectedValueException` when an unarrangeable item is encountered.#### Map to Reordered Items

[](#map-to-reordered-items)

By default, the new order passed to `rearrange()` will map to the collection's keys. However, you may pass any callable as the 3rd parameter to the method to map the order values yourself:

```
Collection::make([ 'a', 'b', 'c' ]);
    ->rearrange(
        [ 'b', 'a', 'c' ],
        Collection::UNARRANGEABLE_APPEND,
        function ($value, $key) {
            return $value;
        }
    ),
    ->all() === [ 'b', 'a', 'c' ];
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2755d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b1da49bad9203cc40a34d975dc00c80af09452bee1da6413e2f1b8214b539675?d=identicon)[loilo](/maintainers/loilo)

---

Top Contributors

[![loilo](https://avatars.githubusercontent.com/u/1922624?v=4)](https://github.com/loilo "loilo (9 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/loilo-collection/health.svg)

```
[![Health](https://phpackages.com/badges/loilo-collection/health.svg)](https://phpackages.com/packages/loilo-collection)
```

###  Alternatives

[jofrysutanto/windsor

YAML-ised Configuration for ACF

549.2k](/packages/jofrysutanto-windsor)[jshannon63/jsoncollect

Supercharge your JSON using collections

154.9k1](/packages/jshannon63-jsoncollect)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
