Download the PHP package derheyne/laravel-list without Composer

On this page you can find all versions of the php package derheyne/laravel-list. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-list

A Laravel Collection that guarantees sequential, 0-based integer keys at all times.

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Laravel List

ListCollection is a drop-in replacement for Illuminate\Support\Collection that enforces a single invariant: keys are always [0, 1, 2, ...]. Every operation that would otherwise leave gaps re-indexes automatically, and operations that fundamentally produce associative keys throw a BadMethodCallException instead of silently changing the shape of your data.

Why?

Laravel's Collection preserves keys through almost every operation. After filter(), sort(), unique(), or forget() you are typically left with gaps:

This is the right default for a general-purpose collection, but it is the wrong default any time the data is conceptually a list -- an API response, a frontend payload, an indexed sequence, anywhere a "missing" key would be a bug. Defending against it manually means sprinkling ->values() calls through your code and remembering to do so every time.

ListCollection removes that whole class of bug:

Installation

There is no service provider, facade, or configuration. Use the class directly wherever you would use a Collection.

Usage

Creating a list

The constructor and all static factories normalise their input to a sequential list. Associative keys, sparse integer keys, and Collection inputs are all flattened to [0, 1, 2, ...]:

The package also ships a global list_of() helper, the ListCollection analogue of Laravel's collect(). It accepts either a single iterable / Arrayable / Collection, or any number of variadic items:

Filtering, sorting, deduplicating

These all return a new ListCollection with re-indexed keys -- no ->values() required:

Adding, removing, replacing

forget(), prepend(), transform(), pull(), push(), pop(), shift(), and splice() mutate in place. Methods like filter(), map(), sort(), slice(), and concat() return a new instance. This is the same split as Illuminate\Support\Collection.

Setting values by index

offsetSet is constrained to keep the list contiguous. Valid replacement indices are 0 through count($list); anything else appends:

This means $list[$i] = $value is always safe -- it can never punch a hole in the list or create a string key.

Transformations

partition() and chunk() recursively return ListCollection instances, so the invariant holds at every level.

Pulling values

pull() removes an item by index, returns it, and re-indexes the remainder. Defaults can be a value or a closure:

pluck()

pluck() works as usual when you only ask for values:

Calling pluck() with the second $key argument throws a BadMethodCallException, because using one column as keys inherently produces an associative result.

JSON serialisation

Because keys never have gaps, toJson() always produces a JSON array, never an object:

This is the most common reason to reach for ListCollection in a Laravel app -- API responses, resource collections, and frontend payloads stay consistent regardless of what filtering or sorting happens upstream.

Blocked methods

The following methods are blocked with a BadMethodCallException. Each one inherently produces associative keys, so silently re-indexing would discard information the caller asked for:

Method Why it is blocked
flip() Uses values as keys
combine($values) Uses current items as keys
groupBy($groupBy) Groups into an associative structure
keyBy($keyBy) Re-keys by a field or callback
countBy($countBy) Counts into an associative structure
mapWithKeys($callback) The callback defines the keys
mapToDictionary($callback) Produces a dictionary structure
mapToGroups($callback) Produces a grouped structure
pluck($value, $key) Only when $key is given -- it would re-key by that field

If you genuinely need one of these shapes, hand the data off to a regular Collection:

All supported Collection methods

Everything else on Illuminate\Support\Collection works and keeps the invariant. A non-exhaustive list:

add, all, chunk, collapse, concat, contains, count, diff, each, every, except, filter, first, firstWhere, flatMap, flatten, forget, get, implode, intersect, isEmpty, isNotEmpty, join, last, map, max, median, merge, min, nth, only, pad, partition, pipe, pluck (without $key), pop, prepend, pull, push, random, reduce, reject, replace, reverse, search, shift, shuffle, skip, skipUntil, skipWhile, slice, sole, some, sort, sortBy, sortByDesc, sortDesc, sortKeys, sortKeysDesc, splice, sum, take, takeUntil, takeWhile, toArray, toJson, transform, unique, unless, values, when, where, whereIn, whereNotIn, zip.

Type safety

ListCollection is annotated as @extends Collection<int, TValue>, so PHPStan and your IDE understand that keys are always int and values keep their type through map(), filter(), pluck(), and friends. The package itself is analysed at PHPStan level: max.

Requirements

Testing

The test suite runs on PHP 8.3 and 8.4 against Laravel 11 and 12, on both Ubuntu and Windows.

Changelog

Please see CHANGELOG for recent changes.

Contributing

Pull requests are welcome. Please run composer test and composer analyse before opening a PR, and add a Pest test for any new behaviour.

Credits

License

The MIT License (MIT). Please see the License File for more information.


All versions of laravel-list with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0
illuminate/collections Version ^11.0||^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package derheyne/laravel-list contains the following files

Loading the files please wait ...