Laravel Collection Cheat Sheet

Laravel Laravel

Laravel’s Collection class provides a fluent, convenient wrapper for working with arrays of data. This cheat sheet summarizes commonly used collection methods with examples.

Introduction

Collections in Laravel are instances of Illuminate\Support\Collection. They provide powerful methods to manipulate arrays with a clean, chainable syntax.

use Illuminate\Support\Collection;

$collection = collect([1, 2, 3, 4, 5]);

🔁 Basic Methods

all()

Get the underlying array.

$collection->all();

count()

Count the number of items.

$collection->count();

isEmpty() / isNotEmpty()

Check if collection is empty or not.

$collection->isEmpty();
$collection->isNotEmpty();

first() / last()

Get the first or last item.

$collection->first();
$collection->last();

🔍 Filtering & Searching

filter()

Filter using a callback.

$collection->filter(fn($value) => $value > 2);

where()

Filter items by a key-value pair (useful on collection of arrays or models).

$users->where('active', true);

contains()

Check if collection contains a value or key/value.

$collection->contains(3);
$collection->contains('name', 'John');

search()

Get the key of the first matching item.

$collection->search('apple');

🔁 Transformation

map()

Apply callback to each item.

$collection->map(fn($item) => $item * 2);

each()

Iterate over each item (does not return a new collection).

$collection->each(fn($item) => Log::info($item));

pluck()

Get values of a given key from a collection of arrays/objects.

$users->pluck('name');

only() / except()

Keep or remove specific keys.

$collection->only(['name', 'email']);
$collection->except(['password']);

🔄 Sorting & Ordering

sort()

Sort values (preserving keys).

$collection->sort();

sortBy() / sortByDesc()

Sort by a key or computed value.

$collection->sortBy('price');
$collection->sortByDesc(fn($item) => strlen($item));

reverse()

Reverse item order.

$collection->reverse();

🧮 Aggregates & Math

sum() / avg() / min() / max()

Basic aggregation.

$collection->sum();
$collection->avg();
$collection->min();
$collection->max();

reduce()

Reduce collection to a single value.

$collection->reduce(fn($carry, $item) => $carry + $item, 0);

🎛️ Utility Methods

chunk()

Split collection into chunks.

$collection->chunk(2);

merge()

Merge with another array/collection.

$collection->merge([6, 7]);

concat()

Append values without overwriting keys.

$collection->concat([6, 7]);

unique()

Remove duplicate values.

$collection->unique();

flatten()

Flatten a multi-dimensional collection.

$collection->flatten();

🧠 Tips & Best Practices

  • Always remember: most methods return a new collection, they don’t mutate the original one.
  • Use ->toArray() when you need raw array output.
  • Chain methods fluently for readability.
$collection
    ->filter(fn($item) => $item > 2)
    ->map(fn($item) => $item * 10)
    ->values();

🧩 Useful Links


Want to see more cheat sheets? Visit Thousand Tech Blog for practical Laravel and PHP articles!

コメント

Copied title and URL