Laravel Collections Tutorial: Working With Arrays the Easy Way
Learn Laravel Collections — a powerful, fluent wrapper around arrays with methods like map, filter, sortBy, and pluck, explained simply.
Every time you run Model::all() or Model::get() , Laravel doesn't return a plain PHP array — it returns a Collection . Collections wrap arrays with dozens of convenient, readable methods for filtering, transforming, and analyzing data. Creating a Collection use Illuminate\Support\Collection; $collection = collect([1, 2, 3, 4, 5]); Eloquent results are already collections automatically: $products = Product::all(); // this is a Collection Filtering Data $expensive = $products->filter(function ($product) { return $product->price > 100; }); Transforming Data With map $names = $products->map(function ($product) { return strtoupper($product->name); }); Extracting a Single Column With pluck $names = $products->pluck('name'); Sorting $sorted = $products->sortBy('price'); $sortedDesc = $products->sortByDesc('price'); Grouping Data $grouped = $products->groupBy('categ...
Home · Projects · Articles · Developer tools · Contact