What is Laravel List?
Laravel List is a lightweight Laravel package that providesListCollection, a specialized collection class that enforces sequential 0-based integer keys (0, 1, 2, 3…) at all times. It extends Laravel’s powerful Collection class while guaranteeing true list behavior.
The Problem It Solves
Laravel’s standardCollection class is incredibly versatile, but this flexibility can lead to unexpected behavior when you need predictable, sequential keys:
ListCollection, keys are automatically reindexed after every operation:
Key Features
Automatic Reindexing
All operations that modify keys automatically reindex to maintain sequential 0-based keys. No manual
values() calls needed.Laravel Collection API
Extends Laravel’s
Collection class, so you get all the familiar methods like map(), filter(), sort(), and more.Type-Safe
Full PHP 8.3+ type hints and generics support with PHPStan for robust, maintainable code.
Predictable JSON
Always serializes to JSON arrays, never objects. Perfect for APIs and frontend integration.
Protection from Associative Operations
Methods that would produce associative keys (like
flip(), groupBy(), keyBy()) throw exceptions to prevent misuse.Zero Configuration
Works out of the box with Laravel 11+ and PHP 8.3+. No configuration files or migrations required.
When to Use Laravel List
Stick with standard
Collection when you need:- Associative arrays with custom keys
- Key preservation across operations
- Methods like
groupBy(),keyBy(), orflip()
Real-World Example
Imagine building an API endpoint that returns a filtered list of active users:Get Started
Installation
Install Laravel List via Composer and get up and running in seconds.
Quickstart
Learn the basics with a hands-on example that demonstrates core functionality.
Core Concepts
Understand how Laravel List maintains sequential keys and which methods are supported.