Skip to main content

Overview

ListCollection provides static factory methods as convenient alternatives to the constructor. All factory methods return ListCollection instances with sequential integer keys.

make()

Create a new ListCollection instance from an array or iterable.

Signature

public static function make($items = []): static
items
mixed
default:"[]"
The items to include in the collection. Can be an array, Collection, or iterable.
Returns: ListCollection - New collection with sequential keys.

Examples

Create from array

$list = ListCollection::make([1, 2, 3]);

$list->all();
// [0 => 1, 1 => 2, 2 => 3]

Create from associative array

$list = ListCollection::make(['z' => 3, 'a' => 1]);

$list->all();
// [0 => 3, 1 => 1]

Create empty collection

$list = ListCollection::make();

$list->isEmpty();
// true

$list->all();
// []

Usage Pattern

The make() method provides a more fluent alternative to the constructor:
// Using constructor
$list = new ListCollection([1, 2, 3]);

// Using make() - more fluent
$list = ListCollection::make([1, 2, 3]);

times()

Create a collection by invoking a callback a given number of times.

Signature

public static function times($number, callable $callback = null): static
number
int
required
The number of times to invoke the callback.
callback
callable(int): mixed
default:"null"
Callback that receives the iteration number (1-indexed). If omitted, returns the iteration numbers.
Returns: ListCollection - New collection with generated values.

Examples

Generate with callback

$list = ListCollection::times(3, fn(int $i): int => $i * 10);

$list->all();
// [0 => 10, 1 => 20, 2 => 30]

Generate sequence

$list = ListCollection::times(5, fn(int $i): int => $i);

$list->all();
// [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5]

Generate objects

$list = ListCollection::times(3, fn(int $i): array => [
    'id' => $i,
    'name' => "Item {$i}",
]);

$list->all();
// [
//   0 => ['id' => 1, 'name' => 'Item 1'],
//   1 => ['id' => 2, 'name' => 'Item 2'],
//   2 => ['id' => 3, 'name' => 'Item 3'],
// ]

Use Cases

  • Generating test data: Create mock objects for testing
  • Pagination: Generate page numbers
  • Initialization: Create default structures
  • Iteration: Repeat an operation n times

wrap()

Wrap a value in a ListCollection. If already a collection, returns as-is.

Signature

public static function wrap($value): static
value
mixed
required
The value to wrap. Can be a single value, array, or existing collection.
Returns: ListCollection - Collection containing the value(s).

Examples

Wrap single value

$list = ListCollection::wrap('single');

$list->all();
// [0 => 'single']

Wrap array

$list = ListCollection::wrap([1, 2, 3]);

$list->all();
// [0 => 1, 1 => 2, 2 => 3]

Wrap existing collection

$existing = new ListCollection([1, 2, 3]);
$wrapped = ListCollection::wrap($existing);

$wrapped === $existing;
// true (returns same instance)

Wrap null

$list = ListCollection::wrap(null);

$list->all();
// []

Use Cases

  • Defensive programming: Ensure you have a collection
  • API flexibility: Accept single values or arrays
  • Type normalization: Unified handling of various input types
function processItems($items)
{
    $collection = ListCollection::wrap($items);
    
    return $collection->map(fn($item) => process($item));
}

// Works with single value
processItems('one');

// Works with array
processItems(['one', 'two', 'three']);

// Works with collection
processItems(ListCollection::make(['one', 'two']));

Comparison with Constructor

Constructor

$list = new ListCollection([1, 2, 3]);
  • Direct instantiation
  • Requires new keyword
  • Explicit type declaration

make()

$list = ListCollection::make([1, 2, 3]);
  • Static factory method
  • More fluent API
  • Identical behavior to constructor

times()

$list = ListCollection::times(3, fn($i) => $i * 10);
  • Generates values programmatically
  • Callback receives iteration number
  • Useful for creating sequences

wrap()

$list = ListCollection::wrap($unknown);
  • Safely wraps any value
  • Handles collections, arrays, and scalars
  • Returns existing collections as-is

Practical Examples

Generate ID sequence

$ids = ListCollection::times(10, fn($i) => $i)
    ->map(fn($id) => str_pad($id, 4, '0', STR_PAD_LEFT));

$ids->all();
// ['0001', '0002', '0003', ..., '0010']

Create test users

$users = ListCollection::times(5, fn($i) => [
    'id' => $i,
    'name' => "User {$i}",
    'email' => "user{$i}@example.com",
]);

Flexible input handling

function addTags($post, $tags)
{
    $tagCollection = ListCollection::wrap($tags);
    
    foreach ($tagCollection as $tag) {
        $post->tags()->attach($tag);
    }
}

// Works with single tag
addTags($post, 'php');

// Works with multiple tags
addTags($post, ['php', 'laravel', 'collections']);

Key Points

  • All factory methods return ListCollection with sequential keys (0, 1, 2, …)
  • make() is a fluent alternative to the constructor
  • times() generates values programmatically using a callback
  • wrap() safely ensures you have a collection, regardless of input type
  • Factory methods are inherited from Laravel’s Collection but return ListCollection instances
  • All methods respect the automatic reindexing behavior of ListCollection

Build docs developers (and LLMs) love