Last active
November 27, 2020 11:25
-
-
Save javedbaloch4/0339fa9f522e04cf0109539b5b68f0fc to your computer and use it in GitHub Desktop.
Laravel Collection Examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function average() { | |
// Get The Average | |
# Ex 1 | |
return (10 + 20 + 30) / 3; | |
# Ex 2 | |
return collect([10, 20, 30])->average(); | |
# Ex 3 | |
$data = [10, 20, 30]; | |
return collect($data)->average(); | |
# Ex 4 | |
$data = [ | |
['price' => 1000], | |
['price' => 2000], | |
['price'=> 3000] | |
]; | |
return collect($data)->average('price'); | |
# Ex 5 | |
return collect($data)->average(function($value) { | |
if (! $value['active']) { | |
return null; | |
} | |
return $value['price'] + $value['tax']; | |
}); | |
# Ex 6 | |
$students = [ | |
['name' => 'Javed', 'discount_fee' => 10400, 'tax' => 234, 'dropped' => false], | |
['name' => 'Kamran', 'discount_fee' => 83773, 'tax' => 544, 'dropped' => false], | |
['name' => 'Umar', 'discount_fee' => 10000, 'tax' => 234, 'dropped' => false], | |
]; | |
return collect($students)->average(function($value) { | |
if (! $$value['dropped']) | |
return null; | |
return $value['dropped'] + $value['tax']; | |
}); | |
} | |
public function max() { | |
$courses = [ | |
['coruse'=>'php', 'fee'=>10000, 'discount' => 1000, 'active' => false], | |
['coruse'=>'react', 'fee'=>12000, 'discount' => 1100, 'active' => false], | |
['coruse'=>'nodejs', 'fee'=>15000, 'discount' => 1500, 'active' => true], | |
['coruse'=>'ai', 'fee'=>13000, 'discount' => 1200, 'active' => true], | |
]; | |
return collect($courses)->max(function($value) { | |
if (! $value['active']) | |
return null; | |
return $value['fee'] - $value['discount']; | |
}); | |
} | |
public function min() { | |
$courses = [ | |
['coruse' => 'php', 'fee' => 10000, 'discount' => 1000, 'active' => false], | |
['coruse' => 'react', 'fee' => 12000, 'discount' => 1100, 'active' => true], | |
['coruse' => 'nodejs', 'fee '=> 15000, 'discount' => 1500, 'active' => true], | |
['coruse' => 'ai', 'fee' => 13000, 'discount' => 1200, 'active' => true], | |
]; | |
return collect($courses)->min(function($value) { | |
if (! $value['active']) | |
return null; | |
return $value['discount'] + 1000 * 2; | |
}); | |
} | |
public function collapse() { | |
$data = [ | |
[0, 1, 4, 4], | |
[0,0,1,3, 5], | |
['a','b','c','d'], | |
[ | |
0 => 50, | |
1 => 30 | |
], | |
[4,6,3,1, 6], | |
[9,4,5, 2], | |
]; | |
// return collect($data)->collapse(); | |
return collect($data)->collapse()->first(); | |
} | |
public function chunk() { | |
$collection = collect([1, 2, 3, 4, 5, 6, 7,8]); | |
$chunks = $collection->chunk(4); | |
return $chunks->all(); | |
} | |
public function combine() { | |
$keys = collect(['name','age']); | |
$user = $keys->combine(['javed', 20]); | |
return $user; | |
} | |
public function concat() { | |
$data = ['key'=>'value1']; | |
$appendData = ['key2'=>'value2','Value3','Value4']; | |
return collect($data)->concat($appendData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment