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// The array we're going to return
2 $data = [];
3// Query the users table
4$query = users::where('id', 1)->get();
5
6// Let's Map the results from [$query]
7$map = $query->map(
8 function($items){
9 $data['user_firstName'] = $items->firstName;
10 $data['user_lastName'] = $items->lastName;
11 return $data;
12 }
13 );
14
15return $map;
1$itemCollection = collect($contacts);
2$filtered = $itemCollection->filter(function($item) use ($search) {
3 return stripos($item['username'],$search) !== false;
4});
5
1$collection = collect([1, 2, 3]);
2
3$total = $collection->reduce(function ($carry, $item) {
4 return $carry + $item;
5});
6
7// 6
8
9$total = $collection->reduce(function ($carry, $item) {
10 return $carry + $item;
11}, 4);
12
13// 10 | where 4 is a initial value
1$collection = collect([1,2,3,4]);
2
3$collection->each(function($item){
4 return $item*$item;
5});
6
7// [1,4,9,16]
1public function index()
2{
3 $myStudents = [
4 ['id'=>1, 'name'=>'Hardik', 'mark' => 80],
5 ['id'=>2, 'name'=>'Paresh', 'mark' => 20],
6 ['id'=>3, 'name'=>'Akash', 'mark' => 34],
7 ['id'=>4, 'name'=>'Sagar', 'mark' => 45],
8 ];
9
10 $myStudents = collect($myStudents);
11
12 $passedstudents = $myStudents->filter(function ($value, $key) {
13 return data_get($value, 'mark') > 34;
14 });
15
16 $passedstudents = $passedstudents->all();
17
18 dd($passedstudents);
19}