34
loading...
This website collects cookies to deliver better user experience
$collection = collect([
[
'id' => 1,
'name' => 'john',
'age' => 52,
'home_owner' => true,
'kids' => 4,
'type' => User::class
],
[
'id' => 2,
'name' => 'sarah',
'age' => 23,
'home_owner' => false,
'kids' => 1,
'type' => User::class
],
[
'id' => 3,
'name' => 'tim',
'age' => 28,
'home_owner' => false,
'kids' => 2,
'type' => User::class
],
[
'id' => 4,
'name' => 'sam',
'age' => 68,
'home_owner' => true,
'kids' => 2,
'type' => User::class
],
[
'id' => 5,
'name' => 'ray',
'age' => 22,
'home_owner' => false,
'kids' => 2,
'type' => User::class
],
])
returns the maximum value of a given key
$collection->max('age');
5
returns the minimum value of a given key
$collection->min('kids');
4
returns the average value of a given key
$collection->avg('age');
38.6
$collection->average('age');
38.6
returns the median value of a given key
$collection->median('id');
3
returns the mode (most often) value of a given key
$collection->mode('kids');
[0 => 2]
returns the sum value of a given key
$collection->sum('kids');
11
sorts the collection by the given key, keeping the original array keys
$collection->sortBy('age');
// Collection
[
[
'id' => 5,
'name' => 'ray',
'age' => 22,
'home_owner' => false,
'kids' => 2,
'type' => User::class
],
[
'id' => 2,
'name' => 'sarah',
'age' => 23,
'home_owner' => false,
'kids' => 1,
'type' => User::class
],
[
'id' => 3,
'name' => 'tim',
'age' => 28,
'home_owner' => false,
'kids' => 2,
'type' => User::class
],
[
'id' => 1,
'name' => 'john',
'age' => 52,
'home_owner' => true,
'kids' => 4,
'type' => User::class
],
[
'id' => 4,
'name' => 'sam',
'age' => 68,
'home_owner' => true,
'kids' => 2,
'type' => User::class
],
]
sorts the collection descending by the given key, keeping the original array keys
$collection->sortByDesc('age');
// Collection
[
[
'id' => 4,
'name' => 'sam',
'age' => 68,
'home_owner' => true,
'kids' => 2,
'type' => User::class
],
[
'id' => 1,
'name' => 'john',
'age' => 52,
'home_owner' => true,
'kids' => 4,
'type' => User::class
],
[
'id' => 3,
'name' => 'tim',
'age' => 28,
'home_owner' => false,
'kids' => 2,
'type' => User::class
],
[
'id' => 2,
'name' => 'sarah',
'age' => 23,
'home_owner' => false,
'kids' => 1,
'type' => User::class
],
[
'id' => 5,
'name' => 'ray',
'age' => 22,
'home_owner' => false,
'kids' => 2,
'type' => User::class
],
]
creates a new collection consisting of every n-th element:
$collection->nth(2);
// Collection
[
[
'id' => 1,
'name' => 'john',
'age' => 52,
'home_owner' => true,
'kids' => 4,
'type' => User::class
],
[
'id' => 3,
'name' => 'tim',
'age' => 28,
'home_owner' => false,
'kids' => 2,
'type' => User::class
],
[
'id' => 5,
'name' => 'ray',
'age' => 22,
'home_owner' => false,
'kids' => 2,
'type' => User::class
]
]
retrieves the first element of the collection or optionally the first element that meets the condition from a callback function
$collection->first();
[
'id' => 1,
'name' => 'john',
'age' => 52,
'home_owner' => true,
'kids' => 4,
'type' => User::class
]
$collection->first(element => element->id > 1);
[
'id' => 2,
'name' => 'sarah',
'age' => 23,
'home_owner' => false,
'kids' => 1,
'type' => User::class
],
retrieves the last element of the collection or optionally the last element that meets the condition from a callback function
$collection->last();
[
'id' => 5,
'name' => 'ray',
'age' => 22,
'home_owner' => false,
'kids' => 2,
'type' => User::class
]
$collection->last(element => element->id < 5);
[
'id' => 4,
'name' => 'sam',
'age' => 68,
'home_owner' => true,
'kids' => 2,
'type' => User::class
]