1$collection = collect([1, 2, 3, 4]);
2
3$filtered = $collection->filter(function ($value, $key) {
4 return $value > 2;
5});
6
7$filtered->all();
8
9// [3, 4]
1$collection = collect([
2 ['name' => 'Desk', 'price' => 200],
3 ['name' => 'Chair', 'price' => 100],
4 ['name' => 'Bookcase', 'price' => 150],
5]);
6
7$sorted = $collection->sortBy('price');
8
9$sorted->values()->all();
10
11/*
12 [
13 ['name' => 'Chair', 'price' => 100],
14 ['name' => 'Bookcase', 'price' => 150],
15 ['name' => 'Desk', 'price' => 200],
16 ]
17*/
1$collection = collect([1, 2, 3, 4, 5]);
2
3$collection->random();
4
5// 4 - (retrieved randomly)