1$collection = collect([1,2,3,4]);
2
3$collection->each(function($item){
4 return $item*$item;
5});
6
7// [1,4,9,16]
1$collection = collect([1, 2, 3]);
2
3$collection->when(true, function ($collection) {
4 return $collection->push(4);
5});
6
7$collection->all();
8
9// [1, 2, 3, 4]
1$collection = collect([
2 ['product' => 'Desk', 'price' => 200],
3 ['product' => 'Chair', 'price' => 100],
4 ['product' => 'Bookcase', 'price' => 150],
5 ['product' => 'Door', 'price' => 100],
6]);
7
8$filtered = $collection->where('price', 100);
9
10$filtered->all();
11
12/*
13 [
14 ['product' => 'Chair', 'price' => 100],
15 ['product' => 'Door', 'price' => 100],
16 ]
17*/
1$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
2
3$value = $collection->get('name');
4
5// taylor
1$collection = collect([0, 1, 2, 3, 4, 5]);
2
3$chunk = $collection->take(3);
4
5$chunk->all();
6
7// [0, 1, 2]