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!

著者

30 years of experience as a web engineer, currently working as a freelance backend engineer.

PHP: ~30 years (Laravel 7 years / FuelPHP 5 years / CakePHP / custom frameworks)
JavaScript: ~20 years (React & Vue, 4 years each)
Cloud & Infrastructure: AWS (EC2, CloudFront, RDS, API Gateway, etc.) / GCP (BigQuery)

I have been writing PHP since version 4, back when the framework ecosystem was fragmented
and every team had their own approach. I've lived through the evolution firsthand —
from raw PHP and homegrown frameworks to the modern Laravel era —
which means I don't just know how to use a tool, but why it exists and what problem it replaced.

I work across system design, implementation, and operations, primarily on backend systems
for both product companies and contract-based projects.

On this blog, I write about the things I actually got stuck on, looked up, or figured out
in real-world projects. If something here unblocks even one person's day, that's enough for me.

千原 耕司をフォローする

役にたったと思ったら応援をお願いします m(._.)m

Laravel
スポンサーリンク
シェアする
千原 耕司をフォローする
Copied title and URL