Collections
Mantle includes Laravel's collections package
with support for all of its methods and with some special customizations to make
it easier to work with WordPress. For example, you can pass a WP_Query object
to the collect helper function and it will automatically convert it to a
collection of WP_Post objects.
Usage
You can use the collect helper function to create a new collection from an
array or an object. You can also use the collect method on any collection
instance to create a new collection from the items in the original collection.
use function Mantle\Support\Helpers\collect;
$collection = collect( [ 1, 2, 3 ] );
You can then use any of the methods available on the collection instance to manipulate the items in the collection.
use function Mantle\Support\Helpers\collect;
$collection = collect( [ 1, 2, 3 ] )->map( function( $item ) {
return $item * 2;
} );
Collections can be treated like arrays, so you can use array accessors to get items from the collection.
use function Mantle\Support\Helpers\collect;
$collection = collect( [ 1, 2, 3 ] );
$item = $collection[0]; // 1
Available Methods
- add
- after
- all
- avg
- average
- before
- chunk
- chunk_while
- collapse
- collect
- combine
- concat
- contains
- contains_one_item
- contains_strict
- count
- count_by
- cross_join
- dd
- diff
- diff_assoc
- diff_assoc_using
- diff_keys
- doesnt_contain
- dump
- duplicates
- duplicates_strict
- each
- each_spread
- empty
- every
- except
- filter
- first
- first_or_fail
- first_where
- flat_map
- flatten
- flip
- for_page
- forget
- get
- get_caching_iterator
- group_by
- has
- implode
- implode_str
- intersect
- intersect_assoc
- intersect_by_keys
- is_empty
- is_not_empty
- join
- key_by
- keys
- last
- make
- map
- map_into
- map_spread
- map_to_dictionary
- map_to_groups
- map_with_keys
- max
- median
- merge
- merge_recursive
- min
- mode
- nth
- only
- only_children
- pad
- partition
- pipe
- pipe_into
- pipe_through
- pluck
- pop
- prepend
- prepend_many
- pull
- push
- put
- random
- reduce
- reduce_spread
- reject
- replace
- replace_recursive
- reverse
- search
- shift
- shuffle
- skip
- skip_until
- skip_while
- slice
- sliding
- sole
- some
- sort
- sort_by
- sort_by_desc
- sort_desc
- sort_keys
- sort_keys_desc
- sort_keys_using
- splice
- split
- split_in
- sum
- take
- take_until
- take_while
- tap
- times
- to_array
- to_base
- to_json
- to_pretty_json
- toArray
- transform
- trim
- undot
- unique
- unique_strict
- unless_empty
- unless_not_empty
- unwrap
- values
- when_empty
- when_not_empty
- where
- where_between
- where_in
- where_in_strict
- where_instance_of
- where_not_between
- where_not_in
- where_not_in_strict
- where_not_null
- where_null
- where_strict
- wrap
- zip
add
The add method adds an item to the collection.
$collection = collect( [ 1, 2, 3 ] )->add( 4 );
after
Get the item after the given item in the collection. You can pass a value or a callback to determine the target item. Returns null if the item is the last in the collection or not found.
$collection = collect( [ 'a', 'b', 'c', 'd' ] );
$after = $collection->after( 'b' ); // 'c'
$after = $collection->after( function( $item ) {
return $item === 'c';
} ); // 'd'
all
The all method gets all of the items in the collection.
$items = collect( [ 1, 2, 3 ] )->all();
avg
Get the average value of a given key.
$average = collect( [ 1, 2, 3 ] )->average();
$average = collect( [
[ 'name' => 'John', 'age' => 30 ],
[ 'name' => 'Jane', 'age' => 25 ],
] )->average( 'age' );
average
Alias for the avg method.
before
Get the item before the given item in the collection. You can pass a value or a callback to determine the target item. Returns null if the item is the first in the collection or not found.
$collection = collect( [ 'a', 'b', 'c', 'd' ] );
$before = $collection->before( 'c' ); // 'b'
$before = $collection->before( function( $item ) {
return $item === 'b';
} ); // 'a'
chunk
Chunk the collection into smaller collections of a given size.
$chunks = collect( [ 1, 2, 3, 4, 5 ] )->chunk( 2 );
// [[1, 2], [3, 4], [5]]
chunk_while
Chunk the collection into groups as long as the given callback returns true. Each group will contain consecutive items until the callback returns false.
$chunks = collect( [1, 2, 3, 4, 5] )->chunk_while( function( $value, $key, $chunk ) {
return $value - $chunk->last() <= 1;
} );
// [[1, 2, 3], [4, 5]]
collapse
Collapse the collection of arrays into a single, flat collection.
$collection = collect( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] )->collapse();
// [1, 2, 3, 4, 5, 6]
collect
Convert the collection to a base Mantle collection instance. Useful if you are working with a subclass and want a plain collection.
$base = $collection->collect();
combine
Combine the values of the collection with the values of another array or collection.
$collection = collect( [ 'name', 'age' ] )->combine( [ 'John', 30 ] );
// ['name' => 'John', 'age' => 30]
concat
Concatenate values of the given array or collection to the collection.
$collection = collect( [ 1, 2, 3 ] )->concat( [ 4, 5, 6 ] );
// [1, 2, 3, 4, 5, 6]
contains
Determine if the collection contains a given item.
$contains = collect( [ 1, 2, 3 ] )->contains( 2 ); // true
contains_one_item
Determine if the collection contains only one item.
$containsOneItem = collect( [ 1 ] )->contains_one_item(); // true
contains_strict
Determine if the collection contains a given key / value pair.
$contains = collect( [ 'name' => 'John' ] )->contains_strict( 'name', 'John' ); // true
count
Get the number of items in the collection.
$count = collect( [ 1, 2, 3 ] )->count(); // 3
count_by
Count the occurrences of values in the collection.
$counts = collect( [ 1, 2, 2, 3, 3, 3 ] )->count_by();
// [1 => 1, 2 => 2, 3 => 3]
cross_join
Cross join with the given lists, returning all possible permutations.
$collection = collect( [ 1, 2 ] )->cross_join( [ 'a', 'b' ] );
dd
Dump the collection and end the script execution.
collect( [ 1, 2, 3 ] )->dd();
diff
Compare the collection against another item or items using strict comparison.
$diff = collect( [ 1, 2, 3 ] )->diff( [ 2, 3, 4 ] );
// [0 => 1]
diff_assoc
Diff the collection with the given items, key and value-wise.
$diff = collect( [ 'name' => 'John' ] )->diff_assoc( [ 'name' => 'Jane' ] );
// ['name' => 'John']
diff_assoc_using
Diff the collection with the given items, using a callback to compute the difference.
$diff = collect( [ 'name' => 'John' ] )->diff_assoc_using( [ 'name' => 'Jane' ], function( $a, $b ) {
return $a === $b;
} );
// ['name' => 'John']
diff_keys
Diff the collection with the given items, key-wise.
$diff = collect( [ 'name' => 'John' ] )->diff_keys( [ 'name' => 'Jane' ] );
doesnt_contain
Determine if the collection does not contain a given key / value pair.
$doesntContain = collect( [ 'name' => 'John' ] )->doesnt_contain( 'name', 'Jane' ); // true
dump
Dump the collection.
collect( [ 1, 2, 3 ] )->dump();
duplicates
Get the items in the collection that have duplicate values.
$duplicates = collect( [ 1, 2, 2, 3, 3, 3 ] )->duplicates();
// [2, 3]
duplicates_strict
Get the items in the collection that have duplicate values.
$duplicates = collect( [ 1, 2, 2, 3, 3, 3 ] )->duplicates_strict();
// [2, 3]
each
Iterate over the items in the collection.
collect( [ 1, 2, 3 ] )->each( function( $item ) {
// ...
} );
each_spread
Iterate over the collection's items, passing each nested item value into the given callback:
collect( [ [ 'John', 30 ], [ 'Jane', 25 ] ] )->each_spread( function( $name, $age ) {
// ...
} );
empty
Create a new, empty collection instance.
$empty = Collection::empty();
every
Determine if all items in the collection pass the given truth test.
$every = collect( [ 1, 2, 3 ] )->every( function( $item ) {
return $item > 0;
} );
except
Get all items except for those with the specified keys.
$collection = collect( [ 'name' => 'John', 'age' => 30 ] )->except( 'age' );
// ['name' => 'John']
filter
Filter the items in the collection using the given callback.
$collection = collect( [ 1, 2, 3 ] )->filter( function( $item ) {
return $item > 1;
} );
first
Get the first item from the collection.
$item = collect( [ 1, 2, 3 ] )->first();
first_or_fail
Get the first item in the collection that passes the given test, or throw an exception if no matching item is found.
use Mantle\Support\Item_Not_Found_Exception;
try {
$item = collect( [1, 2, 3] )->first_or_fail( fn( $item ) => $item > 2 );
} catch ( Item_Not_Found_Exception $e ) {
// Handle not found
}
first_where
Get the first item from the collection where the given key / value pair is true.
$item = collect( [ 'name' => 'John', 'age' => 30 ] )->first_where( 'age', '>', 25 );
flat_map
Map a collection and flatten the result by a single level.
$collection = collect( [ [ 1, 2 ], [ 3, 4 ] ] )->flat_map( function( array $values ) {
return array_map( function( $value ) {
return $value * 2;
}, $values );
} );
flatten
Flatten a multi-dimensional collection into a single dimension.
$collection = collect( [ [ 1, 2 ], [ 3, 4 ] ] )->flatten();
flip
Flip the items in the collection.
$collection = collect( [ 'name' => 'John', 'age' => 30 ] )->flip();
for_page
Get a new collection containing the items for a given page.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->for_page( 2, 2 );
// [3, 4]
forget
Remove an item from the collection by key.
$collection = collect( [ 'name' => 'John', 'age' => 30 ] )->forget( 'age' );
get
Get an item from the collection.
$item = collect( [ 'name' => 'John', 'age' => 30 ] )->get( 'name' );
get_caching_iterator
Get a CachingIterator instance for the collection, which can be useful for advanced iteration scenarios.
$iterator = collect( [1, 2, 3] )->get_caching_iterator();
group_by
Group an associative array by a field or using a callback.
$collection = collect( [
[ 'name' => 'John', 'age' => 30 ],
[ 'name' => 'Jane', 'age' => 25 ],
] )->group_by( 'age' );
has
Determine if an item exists in the collection by key.
$has = collect( [ 'name' => 'John', 'age' => 30 ] )->has( 'name' );
implode
Join the items in the collection.
$joined = collect( [ 'name', 'age' ] )->implode( ', ' );
implode_str
Join the items in the collection into a string and return a Stringable instance.
$string = collect( [ 'a', 'b', 'c' ] )->implode_str( ', ' );
// Stringable: 'a, b, c'
intersect
Intersect the collection with the given items.
... = collect( [ 1, 2, 3 ] )->intersect( [ 2, 3, 4 ] );
intersect_assoc
Intersect the collection with the given items, key and value-wise.
$intersect = collect( [ 'name' => 'John' ] )->intersect_assoc( [ 'name' => 'Jane' ] );
intersect_by_keys
Intersect the collection with the given items by key.
$intersect = collect( [ 'name' => 'John' ] )->intersect_by_keys( [ 'name' => 'Jane' ] );
is_empty
Determine if the collection is empty.
$isEmpty = collect( [] )->is_empty();
is_not_empty
Determine if the collection is not empty.
$isNotEmpty = collect( [ 1, 2, 3 ] )->is_not_empty();
join
Join the items in the collection.
$joined = collect( [ 'name', 'age' ] )->join( ', ' );
key_by
Key an associative array by a field or using a callback.
$collection = collect( [
[ 'name' => 'John', 'age' => 30 ],
[ 'name' => 'Jane', 'age' => 25 ],
] )->key_by( 'name' );
keys
Get the keys of the collection items.
$keys = collect( [ 'name' => 'John', 'age' => 30 ] )->keys();
last
Get the last item from the collection.
$item = collect( [ 1, 2, 3 ] )->last();
make
Create a new collection instance from the given items.
$collection = Collection::make( [1, 2, 3] );
map
Map the items in the collection to a new callback.
$collection = collect( [ 1, 2, 3 ] )->map( function( $item ) {
return $item * 2;
} );
map_into
Map the collection into a new class.
$collection = collect( [ 'John', 'Jane' ] )->map_into( User::class );
map_spread
Iterates over the collection's items, passing each nested item value into the given callback:
$collection = collect( [ [ 'John', 30 ], [ 'Jane', 25 ] ] )->map_spread( function( $name, $age ) {
return $name . ' is ' . $age . ' years old';
} );
map_to_dictionary
Map the collection to a dictionary.
$dictionary = collect( [
[
'name' => 'John',
'department' => 'Sales',
],
[
'name' => 'Jane',
'department' => 'Marketing',
],
[
'name' => 'Jack',
'department' => 'Sales',
],
] )->map_to_dictionary( function ( $item ) {
return [
$item['department'] => $item['name'],
];
} ); // [ 'Marketing' => ['John', 'Jane'], 'Sales' => ['Jack']]
You can also return false/null to skip an item.
$dictionary = collect( [
[
'name' => 'John',
'department' => 'Sales',
],
[
'name' => 'Jane',
'department' => 'Marketing',
],
[
'name' => 'Jack',
'department' => 'Sales',
],
[
'name' => 'Adam',
'department' => 'Tech',
],
]
] )->map_to_dictionary( function ( $item ) {
if ( $item['department'] === 'Sales' ) {
return false;
}
return [
$item['department'] => $item['name'],
];
} ); // ['Marketing' => ['Jane'], 'Tech' => ['Adam']]
map_to_groups
Alias to map_to_dictionary.
map_with_keys
Map the collection with the given key / value pairs.
$collection = collect( [
[
'name' => 'John',
'department' => 'Sales',
'email' => 'john@example.com',
],
[
'name' => 'Jane',
'department' => 'Marketing',
'email' => 'jane@example.com',
]
] );
$collection = $collection->map_with_keys( function( $item ) {
return [ $item['name'] => $item['email'] ];
} );
max
Get the max value of a given key.
$max = collect( [ 1, 2, 3 ] )->max();
median
Get the median value of a given key.
$median = collect( [ 1, 2, 3 ] )->median();
merge
Merge the collection with the given items.
$collection = collect( [ 'name' => 'John' ] )->merge( [ 'name' => 'Jane' ] );
merge_recursive
Merge the collection with the given items, recursively.
$collection = collect( [ 'name' => 'John' ] )->merge_recursive( [ 'name' => 'Jane' ] );
min
Get the min value of a given key.
$min = collect( [ 1, 2, 3 ] )->min();
mode
Get the mode value of a given key.
$mode = collect( [ 1, 2, 2, 3, 3, 3 ] )->mode();
nth
Create a new collection consisting of every n-th element.
$collection = collect( [ 1, 2, 3, 4, 5, 6 ] )->nth( 2 );
// [2, 4, 6]
only
Create a new collection consisting of the given keys.
$collection = collect( [
'name' => 'John',
'department' => 'Sales',
] )->only( [ 'name' ] );
only_children
Create a new collection consisting of only keys that are children of the given keys.
$collection = collect( [
[
'name' => 'John',
'department' => 'Sales',
'email' => 'john@example.com',
],
[
'name' => 'Jane',
'department' => 'Marketing',
'email' => 'jane@example.com',
]
] );
$collection = $collection->only_children( [ 'email', 'name' ] );
// [['name' => 'John', 'email' => 'john@example.com'], ['name' => 'Jane', 'email' => 'jane@example.com']]
pad
Pad the collection to the specified size with a value.
$collection = collect( [ 1, 2, 3 ] )->pad( 5, 0 );
// [1, 2, 3, 0, 0]
partition
Partition the collection into two arrays using the given callback or key.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->partition( function( $value, $key ) {
return $value > 2;
} );
pipe
Pass the collection to the given callback and return the result.
$sum = collect( [1, 2, 3] )->pipe( fn( $collection ) => $collection->sum() );
// 6
pipe_into
Pass the collection into a new class instance.
class Stats {
public function __construct( $collection ) {
$this->total = $collection->sum();
}
}
$stats = collect( [1, 2, 3] )->pipe_into( Stats::class );
// $stats->total === 6
pipe_through
Pass the collection through a series of callbacks and return the result.
$result = collect( [1, 2, 3] )->pipe_through([
fn( $c ) => $c->map( fn( $i ) => $i * 2 ),
fn( $c ) => $c->sum(),
]);
// 12
pluck
Get an array with the values of a given key.
$plucked = collect( [
[ 'name' => 'John', 'department' => 'Sales' ],
[ 'name' => 'Jane', 'department' => 'Marketing' ],
] )->pluck( 'name' );
pop
Remove and return the last item from the collection.
$item = collect( [ 1, 2, 3 ] )->pop(); // 3
prepend
Prepend an item to the beginning of the collection.
$collection = collect( [ 1, 2, 3 ] )->prepend( 0 );
prepend_many
Prepend multiple items to the beginning of the collection.
$collection = collect( [ 1, 2, 3 ] )->prepend_many( [ 0, -1, -2 ] );
// [0, -1, -2, 1, 2, 3]
pull
Removes and returns an item from the collection by key.
$item = collect( [ 'name' => 'John' ] )->pull( 'name' );
push
Push an item onto the end of the collection.
$collection = collect( [ 1, 2, 3 ] )->push( 4 );
put
Put an item in the collection by key.
$collection = collect( [ 'name' => 'John' ] )->put( 'age', 30 );
random
Return a random item from the collection.
$random = collect( [ 1, 2, 3 ] )->random();
reduce
Reduce the collection to a single value.
$reduced = collect( [ 1, 2, 3 ] )->reduce( function( $carry, $item ) {
return $carry + $item;
} );
reduce_spread
Reduce the collection to multiple aggregate values at once.
[$min, $max] = collect( [1, 2, 3] )->reduce_spread( function( $min, $max, $value ) {
return [
min( $min, $value ),
max( $max, $value ),
];
}, PHP_INT_MAX, PHP_INT_MIN );
// $min = 1, $max = 3
reject
Filter items in the collection using the given callback.
$collection = collect( [ 1, 2, 3 ] )->reject( function( $item ) {
return $item > 1;
} );
replace
Replace items in the collection with the given items.
$collection = collect( [ 'name' => 'John' ] )->replace( [ 'name' => 'Jane' ] );
replace_recursive
Replace items in the collection with the given items, recursively.
$collection = collect( [ 'name' => 'John' ] )->replace_recursive( [ 'name' => 'Jane' ] );
reverse
Reverse items in the collection.
$collection = collect( [ 1, 2, 3 ] )->reverse();
search
Search the collection for a given value and return the corresponding key if successful.
$key = collect( [ 'name' => 'John' ] )->search( 'John' );
shift
Remove and return the first item from the collection.
$item = collect( [ 1, 2, 3 ] )->shift(); // 1
shuffle
Shuffle the items in the collection.
$collection = collect( [ 1, 2, 3 ] )->shuffle();
skip
Skip the first n items.
$collection = collect( [ 1, 2, 3 ] )->skip( 2 );
skip_until
Skip items in the collection until the given condition is met.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->skip_until( function( $value ) {
return $value >= 3;
} );
// [3, 4, 5]
$collection = collect( [ 1, 2, 3, 4, 5 ] )->skip_until( 3 );
// [3, 4, 5]
skip_while
Skip items in the collection while the given condition is met.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->skip_while( function( $value ) {
return $value <= 3;
} );
// [4, 5]
$collection = collect( [ 1, 2, 3, 4, 5 ] )->skip_while( function( $value ) {
return $value < 3;
} );
// [3, 4, 5]
slice
Slice the collection by the given offset and length.
$collection = collect( [ 1, 2, 3 ] )->slice( 1, 2 ); // [2, 3]
sliding
Create a "sliding window" view of the items in the collection.
$windows = collect( [1, 2, 3, 4] )->sliding( 2 );
// [[1, 2], [2, 3], [3, 4]]
sole
Get the first item in the collection, but only if exactly one item exists. Throws an exception otherwise.
use Mantle\Support\Item_Not_Found_Exception;
use Mantle\Support\Multiple_Items_Found_Exception;
try {
$item = collect( [42] )->sole();
} catch ( Item_Not_Found_Exception | Multiple_Items_Found_Exception $e ) {
// Handle error
}
some
Alias for the contains method.
sort
Sort the collection with an optional callback.
$collection = collect( [ 3, 2, 1 ] )->sort();
sort_by
Sort the collection by the given callback.
$collection = collect( [ 1, 3, 2 ] )->sort_by( function( $item ) {
return $item;
} );
sort_by_desc
Sort the collection by the given callback in descending order.
$collection = collect( [ 3, 2, 1 ] )->sort_by_desc( function( $item ) {
return $item;
} );
sort_desc
Sort the collection in descending order.
$collection = collect( [ 3, 2, 1 ] )->sort_desc();
sort_keys
Sort the collection by keys.
$collection = collect( [ 'b' => 2, 'a' => 1 ] )->sort_keys();
sort_keys_desc
Sort the collection by keys in descending order.
$collection = collect( [ 'b' => 2, 'a' => 1 ] )->sort_keys_desc();
sort_keys_using
Sort the collection keys using a custom callback.
$collection = collect( [ 'b' => 2, 'a' => 1 ] )->sort_keys_using( function( $a, $b ) {
return strcmp( $a, $b );
} );
// ['a' => 1, 'b' => 2]
splice
Remove and return a portion of the collection.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->splice( 1, 2 );
split
Split the collection into the given number of groups.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->split( 2 ); // [[1, 2, 3], [4, 5]]
split_in
Split the collection into the given number of groups, filling the first groups completely.
$groups = collect( [1, 2, 3, 4, 5] )->split_in( 2 );
// [[1, 2, 3], [4, 5]]
sum
Get the sum of the given values.
$sum = collect( [ 1, 2, 3 ] )->sum();
$sum = collect( [
[ 'name' => 'John', 'age' => 30 ],
[ 'name' => 'Jane', 'age' => 25 ],
] )->sum( 'age' );
take
Take the first or last n items.
$collection = collect( [ 1, 2, 3 ] )->take( 2 );
take_until
Take items in the collection until the given condition is met. Stops before the item that matches the condition. You can pass a value or a callback.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->take_until( function( $value ) {
return $value >= 4;
} );
// [1, 2, 3]
$collection = collect( [ 1, 2, 3, 4, 5 ] )->take_until( 3 );
// [1, 2]
take_while
Take items in the collection while the given condition is met. You can pass a value or a callback.
$collection = collect( [ 1, 2, 3, 4, 5 ] )->take_while( function( $value ) {
return $value < 4;
} );
// [1, 2, 3]
$collection = collect( [ 1, 1, 2, 3, 2 ] )->take_while( 1 );
// [1, 1]
tap
Pass the collection to the given callback and then return the collection.
$collection = collect( [1, 2, 3] )->tap( function( $c ) {
// Do something with $c
} );
times
Create a new collection by invoking the callback a given number of times.
$collection = Collection::times( 3, fn( $i ) => $i * 2 );
// [2, 4, 6]
to_array
Convert the collection to a plain array.
$array = collect( [ 1, 2, 3 ] )->to_array();
to_base
Get a base Mantle collection instance from this collection.
$base = $collection->to_base();
to_json
Convert the collection to its JSON representation.
$json = collect( [ 1, 2, 3 ] )->to_json();
to_pretty_json
Get the collection as pretty-printed JSON.
$json = collect( [1, 2, 3] )->to_pretty_json();
toArray
Alias for the to_array methods.
transform
Transform each item in the collection using a callback (modifies the collection in place).
$collection = collect( [1, 2, 3] )->transform( fn( $item ) => $item * 2 );
// [2, 4, 6]
trim
Trim the collection's string values.
$collection = collect( [ ' John ', ' Jane ' ] )->trim();
undot
Expand a flattened "dot" notation array into an expanded array.
$collection = collect( [ 'user.name' => 'John' ] )->undot();
// [ 'user' => [ 'name' => 'John' ] ]
unique
Filter the collection for unique items.
$collection = collect( [ 1, 2, 2, 3, 3, 3 ] )->unique();
unique_strict
Filter the collection with a strict comparison.
$collection = collect( [ 1, 2, 2, 3, 3, 3 ] )->unique_strict();
unless_empty
Execute a callback if the collection is not empty.
$collection = collect( [ 1, 2, 3 ] )->unless_empty( function( $collection ) {
// ...
} );
unless_not_empty
Execute a callback if the collection is empty.
$collection = collect( [] )->unless_not_empty( function( $collection ) {
// ...
} );
unwrap
Get the underlying array from a collection or arrayable value.
$array = Collection::unwrap( collect( [1, 2, 3] ) );
// [1, 2, 3]
values
Get the values of the collection items.
$values = collect( [ 'name' => 'John', 'age' => 30 ] )->values();
when_empty
Execute a callback if the collection is empty.
$collection = collect( [] )->when_empty( function( $collection ) {
// ...
} );
when_not_empty
Execute a callback if the collection is not empty.
$collection = collect( [ 1, 2, 3 ] )->when_not_empty( function( $collection ) {
// ...
} );
where
Filter the collection by determining if a specific key / value pair is true.
$collection = collect( [ 'name' => 'John' ] )->where( 'name', 'John' );
$collection = collect( [ 'name' => 'John' ] )->where( 'name', '!=', 'John' );
where_between
Filter the collection by determining if a specific item is between two values.
$collection = collect( [
[ 'name' => 'Jack', 'age' => 35 ],
[ 'name' => 'John', 'age' => 30 ],
[ 'name' => 'Jane', 'age' => 25 ],
] )->where_between( 'age', [ 25, 30 ] );
where_in
Filter the collection by determining if a specific key / value pair is in the given array.
$collection = collect( [ 'name' => 'John' ] )->where_in( 'name', [ 'John' ] );
where_in_strict
Filter the collection by determining if a specific key / value pair is in the given array.
$collection = collect( [ 'name' => 'John' ] )->where_in_strict( 'name', [ 'John' ] );
where_instance_of
Filter the collection by determining if a specific item is an instance of the given class.
$collection = collect( [ new User, new Post ] )->where_instance_of( User::class );
where_not_between
Filter the collection by determining if a specific item is not between two values.
$collection = collect( [
[ 'name' => 'Jack', 'age' => 35 ],
[ 'name' => 'John', 'age' => 30 ],
[ 'name' => 'Jane', 'age' => 25 ],
] )->where_not_between( 'age', [ 25, 30 ] );
where_not_in
Filter the collection by determining if a specific key / value pair is not in the given array.
$collection = collect( [ 'name' => 'John' ] )->where_not_in( 'name', [ 'Jane' ] );
where_not_in_strict
Filter the collection by determining if a specific key / value pair is not in the given array.
$collection = collect( [ 'name' => 'John' ] )->where_not_in_strict( 'name', [ 'Jane' ] );
where_not_null
Filter the collection by determining if a specific key / value pair is not null.
$collection = collect( [ 'name' => 'John' ] )->where_not_null( 'name' );
where_null
Filter the collection by determining if a specific key / value pair is null.
$collection = collect( [ 'name' => null ] )->where_null( 'name' );
where_strict
Filter the collection by determining if a specific key / value pair is true.
$collection = collect( [ 'name' => 'John' ] )->where_strict( 'name', 'John' );
wrap
Wrap the given value in a collection if it isn't already one.
$collection = Collection::wrap( 5 );
// [5]
Pass the collection to the given closure and returns the result of the executed closure.
$collection = collect( [ 1, 2, 3 ] );
$piped = $collection->pipe( function ( Collection $collection ) {
return $collection->sum();
} );
// 6
zip
The zip method merges together the values of the given array with the values
of the original collection at their corresponding index: