# 🗂 Using Laravel Collections (map, filter, pluck)
Laravel’s **Collections** are powerful wrappers around arrays that make working with data super convenient. Instead of writing raw loops, you can use collection methods like `map`, `filter`, and `pluck` to transform data cleanly.
---
## 🔧 What is a Collection?
A Collection is basically an array with extra helper methods. You can create one using:
```php
use Illuminate\Support\Collection;
$numbers = collect([1, 2, 3, 4, 5]);
🔹 1. Using map()
map() transforms each item in the collection.
$numbers = collect([1, 2, 3]);
$doubled = $numbers->map(function ($num) {
return $num * 2;
});
dd($doubled->all()); // [2, 4, 6]👉 Great for modifying values without changing the original array.
🔹 2. Using filter()
filter() removes items that don’t match a condition.
$numbers = collect([1, 2, 3, 4, 5]);
$even = $numbers->filter(function ($num) {
return $num % 2 === 0;
});
dd($even->all()); // [2, 4]👉 Useful for selecting only the data you need.
🔹 3. Using pluck()
pluck() extracts values from a given key in a collection of arrays or objects.
$users = collect([
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
]);
$names = $users->pluck('name');
dd($names->all()); // ['Alice', 'Bob']👉 Perfect for grabbing specific fields from a dataset.
🎯 Conclusion
Laravel Collections provide a cleaner, chainable way to work with data.
Use map() to transform items.
Use filter() to remove unwanted items.
Use pluck() to extract specific values.
Once you start using Collections, you’ll rarely go back to writing raw PHP loops!