Enumerable
interface Enumerable implements Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable
Some transformations may return a base collection when an implementation cannot preserve its concrete item contract.
Methods
Get the collection of items as a plain array.
Get the collection of items as JSON.
Create a new instance by invoking the callback a given amount of times.
Create a collection with the given range.
Wrap the given value in a collection if applicable.
Get the underlying items from the given collection if applicable.
Create a new instance with no items.
Get all items in the enumerable.
Alias for the "avg" method.
Get the median of a given key.
Get the mode of a given key.
Collapse the items into a single enumerable.
Alias for the "contains" method.
Determine if an item exists, using strict comparison.
Get the average value of a given key.
Determine if an item exists in the enumerable.
Determine if an item is not contained in the collection.
Dump the collection and end the script.
Dump the collection.
Get the items that are not present in the given items.
Get the items that are not present in the given items, using the callback.
Get the items whose keys and values are not present in the given items, using the callback.
Get the items whose keys are not present in the given items, using the callback.
Retrieve duplicate items.
Retrieve duplicate items using strict comparison.
Execute a callback over each item.
Execute a callback over each nested chunk of items.
Determine if all items pass the given truth test.
Get all items except for those with the specified keys.
Run a filter over each of the items.
Apply the callback if the given "value" is (or resolves to) truthy.
Apply the callback if the collection is empty.
Apply the callback if the collection is not empty.
Apply the callback if the given "value" is (or resolves to) falsy.
Apply the callback unless the collection is empty.
Apply the callback unless the collection is not empty.
Filter items by the given key value pair.
Filter items where the value for the given key is null.
Filter items where the value for the given key is not null.
Filter items by the given key value pair using strict comparison.
Filter items by the given key value pair using strict comparison.
Filter items such that the value of the given key is between the given values.
Filter items such that the value of the given key is not between the given values.
Filter items by the given key value pair.
Filter items by the given key value pair using strict comparison.
Filter the items, removing any items that don't match the given type(s).
Get the first item from the enumerable passing the given truth test.
Get the first item by the given key value pair.
Get a flattened array of the items in the collection.
Flip the values with their keys.
Get an item from the collection by key.
Group an associative array by a field or using a callback.
Key an associative array by a field or using a callback.
Determine if an item exists in the collection by key.
Determine if any of the keys exist in the collection.
Concatenate values of a given key as a string.
Intersect the collection with the given items.
Intersect the collection with the given items, using the callback.
Intersect the collection with the given items with additional index check.
Intersect the collection with the given items with additional index check, using the callback.
Intersect the collection with the given items by key.
Determine if the collection is empty or not.
Determine if the collection is not empty.
Determine if the collection contains a single item, optionally matching the given criteria.
Determine if the collection contains multiple items, optionally matching the given criteria.
Join all items from the collection using a string. The final items can use a separate glue string.
Get the keys of the collection items.
Get the last item from the collection.
Run a map over each of the items.
Run a map over each nested chunk of items.
Run a dictionary map over the items.
Run a grouping map over the items.
Run an associative map over each of the items.
Map a collection and flatten the result by a single level.
Map the values into a new class.
Merge the collection with the given items.
Recursively merge the collection with the given items.
Union the collection with the given items.
Get the min value of a given key.
Get the max value of a given key.
Create a new collection consisting of every n-th element.
Get the items with the specified keys.
"Paginate" the collection by slicing it into a smaller collection.
Partition the collection into two arrays using the given callback or key.
Push all of the given items onto the collection.
Get one or a specified number of items randomly from the collection.
Reduce the collection to a single value.
Reduce the collection to a single value by mutating an initial value.
Reduce the collection to multiple aggregate values.
Replace the collection items with the given items.
Recursively replace the collection items with the given items.
Reverse items order.
Search the collection for a given value and return the corresponding key if successful.
Get the item before the given item.
Get the item after the given item.
Shuffle the items in the collection.
Create chunks representing a "sliding window" view of the items in the collection.
Skip the first {$count} items.
Skip items in the collection until the given condition is met.
Skip items in the collection while the given condition is met.
Get a slice of items from the enumerable.
Split a collection into a certain number of groups.
Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
Get the first item in the collection but throw an exception if no matching items exist.
Chunk the collection into chunks of the given size.
Chunk the collection into chunks with a callback.
Chunk the collection into chunks by comparing adjacent values using the given key or callback.
Split a collection into a certain number of groups, and fill the first groups completely.
Sort through each item with a callback.
Sort items in descending order.
Sort the collection using the given callback.
Sort the collection in descending order using the given callback.
Sort the collection keys.
Sort the collection keys in descending order.
Sort the collection keys using a callback.
Get the sum of the given values.
Take the first or last {$limit} items.
Take items in the collection until the given condition is met.
Take items in the collection while the given condition is met.
Pass the collection to the given callback and then return it.
Pass the enumerable to the given callback and return the result.
Pass the collection into a new class.
Pass the collection through a series of callable pipes and return the result.
Create a collection of all elements that do not pass a given truth test.
Flatten a multi-dimensional associative array with dots.
Convert a flatten "dot" notation array into an expanded array.
Return only unique items from the collection array.
Return only unique items from the collection array using strict comparison.
Reset the keys on the underlying array.
Pad collection to the specified length with a value.
Get the values iterator.
Count the number of items in the collection.
Count the number of items in the collection by a field or using a callback.
Collect the values into a collection.
Convert the object into something JSON serializable.
Get the collection of items as pretty print formatted JSON.
Get a CachingIterator instance.
Convert the collection to its string representation.
Indicate that the model's string representation should be escaped when __toString is invoked.
Add a method to the list of proxied methods.
Dynamically access collection proxies.
Details
at line 1144
array
toArray()
Get the collection of items as a plain array.
at line 1156
string
toJson(int $options = 0)
Get the collection of items as JSON.
at line 46
static Enumerable
make(Arrayable|iterable|null $items = [])
Create a new collection instance if the value isn't one already.
at line 56
static Collection|Enumerable
times(int $number, callable|null $callback = null)
Create a new instance by invoking the callback a given amount of times.
at line 63
static Enumerable
range(int $from, int $to, int $step = 1)
Create a collection with the given range.
at line 73
static Enumerable
wrap(mixed $value)
Wrap the given value in a collection if applicable.
at line 78
static mixed
unwrap(mixed $value)
Get the underlying items from the given collection if applicable.
at line 83
static Enumerable
empty()
Create a new instance with no items.
at line 90
array
all()
Get all items in the enumerable.
at line 97
float|int|null
average(callable|string|null $callback = null)
Alias for the "avg" method.
at line 104
float|int|null
median(string|array|null $key = null)
Get the median of a given key.
at line 112
array|null
mode(string|array|null $key = null)
Get the mode of a given key.
at line 119
Collection|Enumerable
collapse()
Collapse the items into a single enumerable.
at line 126
bool
some(mixed $key, mixed $operator = null, mixed $value = null)
Alias for the "contains" method.
at line 134
bool
containsStrict(mixed $key, mixed $value = null)
Determine if an item exists, using strict comparison.
at line 141
float|int|null
avg(callable|int|string|null $callback = null)
Get the average value of a given key.
at line 148
bool
contains(mixed $key, mixed $operator = null, mixed $value = null)
Determine if an item exists in the enumerable.
at line 153
bool
doesntContain(mixed $key, mixed $operator = null, mixed $value = null)
Determine if an item is not contained in the collection.
at line 163
Enumerable
crossJoin(Arrayable|iterable ...$lists)
Cross join with the given lists, returning all possible permutations.
at line 168
never
dd(mixed ...$args)
Dump the collection and end the script.
at line 175
Enumerable
dump(mixed ...$args)
Dump the collection.
at line 182
Enumerable
diff(mixed $items)
Get the items that are not present in the given items.
at line 190
Enumerable
diffUsing(mixed $items, callable $callback)
Get the items that are not present in the given items, using the callback.
at line 197
Enumerable
diffAssoc(Arrayable|iterable $items)
Get the items whose keys and values are not present in the given items.
at line 205
Enumerable
diffAssocUsing(Arrayable|iterable $items, callable $callback)
Get the items whose keys and values are not present in the given items, using the callback.
at line 212
Enumerable
diffKeys(Arrayable|iterable $items)
Get the items whose keys are not present in the given items.
at line 220
Enumerable
diffKeysUsing(Arrayable|iterable $items, callable $callback)
Get the items whose keys are not present in the given items, using the callback.
at line 230
Collection|Enumerable
duplicates(callable|string|null $callback = null, bool $strict = false)
Retrieve duplicate items.
at line 240
Collection|Enumerable
duplicatesStrict(callable|string|null $callback = null)
Retrieve duplicate items using strict comparison.
at line 248
Enumerable
each(callable $callback)
Execute a callback over each item.
at line 253
Enumerable
eachSpread(callable $callback)
Execute a callback over each nested chunk of items.
at line 260
bool
every(mixed $key, mixed $operator = null, mixed $value = null)
Determine if all items pass the given truth test.
at line 267
Enumerable
except(mixed $keys)
Get all items except for those with the specified keys.
at line 274
Enumerable
filter(callable|null $callback = null)
Run a filter over each of the items.
at line 285
mixed
when(mixed $value, callable|null $callback = null, callable|null $default = null)
Apply the callback if the given "value" is (or resolves to) truthy.
at line 296
mixed
whenEmpty(callable $callback, callable|null $default = null)
Apply the callback if the collection is empty.
at line 307
mixed
whenNotEmpty(callable $callback, callable|null $default = null)
Apply the callback if the collection is not empty.
at line 318
mixed
unless(mixed $value, callable $callback, callable|null $default = null)
Apply the callback if the given "value" is (or resolves to) falsy.
at line 329
mixed
unlessEmpty(callable $callback, callable|null $default = null)
Apply the callback unless the collection is empty.
at line 340
mixed
unlessNotEmpty(callable $callback, callable|null $default = null)
Apply the callback unless the collection is not empty.
at line 345
Enumerable
where(callable|string|null $key, mixed $operator = null, mixed $value = null)
Filter items by the given key value pair.
at line 350
Enumerable
whereNull(string|null $key = null)
Filter items where the value for the given key is null.
at line 355
Enumerable
whereNotNull(string|null $key = null)
Filter items where the value for the given key is not null.
at line 360
Enumerable
whereStrict(callable|string|null $key, mixed $value)
Filter items by the given key value pair using strict comparison.
at line 365
Enumerable
whereIn(string $key, Arrayable|iterable $values, bool $strict = false)
Filter items by the given key value pair.
at line 370
Enumerable
whereInStrict(string $key, Arrayable|iterable $values)
Filter items by the given key value pair using strict comparison.
at line 375
Enumerable
whereBetween(string $key, Arrayable|iterable $values)
Filter items such that the value of the given key is between the given values.
at line 380
Enumerable
whereNotBetween(string $key, Arrayable|iterable $values)
Filter items such that the value of the given key is not between the given values.
at line 385
Enumerable
whereNotIn(string $key, Arrayable|iterable $values, bool $strict = false)
Filter items by the given key value pair.
at line 390
Enumerable
whereNotInStrict(string $key, Arrayable|iterable $values)
Filter items by the given key value pair using strict comparison.
at line 400
Enumerable
whereInstanceOf(string|array $type)
Filter the items, removing any items that don't match the given type(s).
at line 411
mixed
first(callable|null $callback = null, mixed $default = null)
Get the first item from the enumerable passing the given truth test.
at line 418
mixed
firstWhere(callable|string $key, mixed $operator = null, mixed $value = null)
Get the first item by the given key value pair.
at line 425
Collection|Enumerable
flatten(int|float $depth = INF)
Get a flattened array of the items in the collection.
at line 432
Collection|Enumerable
flip()
Flip the values with their keys.
at line 443
mixed
get(mixed $key, mixed $default = null)
Get an item from the collection by key.
at line 460
Collection|Enumerable
groupBy(callable|array|string $groupBy, bool $preserveKeys = false)
Group an associative array by a field or using a callback.
at line 470
Enumerable
keyBy(callable|array|string $keyBy)
Key an associative array by a field or using a callback.
at line 477
bool
has(mixed $key)
Determine if an item exists in the collection by key.
at line 482
bool
hasAny(mixed $key)
Determine if any of the keys exist in the collection.
at line 489
string
implode(callable|string|null $value, string|null $glue = null)
Concatenate values of a given key as a string.
at line 496
Enumerable
intersect(mixed $items)
Intersect the collection with the given items.
at line 504
Enumerable
intersectUsing(mixed $items, callable $callback)
Intersect the collection with the given items, using the callback.
at line 511
Enumerable
intersectAssoc(mixed $items)
Intersect the collection with the given items with additional index check.
at line 519
Enumerable
intersectAssocUsing(mixed $items, callable $callback)
Intersect the collection with the given items with additional index check, using the callback.
at line 526
Enumerable
intersectByKeys(mixed $items)
Intersect the collection with the given items by key.
at line 531
bool
isEmpty()
Determine if the collection is empty or not.
at line 536
bool
isNotEmpty()
Determine if the collection is not empty.
at line 545
bool
hasSole(callable|string|null $key = null, mixed $operator = null, mixed $value = null)
Determine if the collection contains a single item, optionally matching the given criteria.
at line 552
bool
hasMany(callable|string|null $key = null, mixed $operator = null, mixed $value = null)
Determine if the collection contains multiple items, optionally matching the given criteria.
at line 557
string
join(string $glue, string $finalGlue = '')
Join all items from the collection using a string. The final items can use a separate glue string.
at line 564
Collection|Enumerable
keys()
Get the keys of the collection items.
at line 575
mixed
last(callable|null $callback = null, mixed $default = null)
Get the last item from the collection.
at line 585
Collection|Enumerable
map(callable $callback)
Run a map over each of the items.
at line 592
Collection|Enumerable
mapSpread(callable $callback)
Run a map over each nested chunk of items.
at line 605
Enumerable
mapToDictionary(callable $callback)
Run a dictionary map over the items.
The callback should return an associative array with a single key/value pair.
at line 618
Collection|Enumerable
mapToGroups(callable $callback)
Run a grouping map over the items.
The callback should return an associative array with a single key/value pair.
at line 631
Collection|Enumerable
mapWithKeys(callable $callback)
Run an associative map over each of the items.
The callback should return an associative array with a single key/value pair.
at line 642
Collection|Enumerable
flatMap(callable $callback)
Map a collection and flatten the result by a single level.
at line 652
Collection|Enumerable
mapInto(string $class)
Map the values into a new class.
at line 662
Enumerable
merge(mixed $items)
Merge the collection with the given items.
at line 672
Enumerable
mergeRecursive(mixed $items)
Recursively merge the collection with the given items.
at line 682
Enumerable
combine(Arrayable|iterable $values)
Create a collection by using this collection for keys and another for its values.
at line 689
Enumerable
union(mixed $items)
Union the collection with the given items.
at line 699
mixed
min(callable|int|string|null $callback = null)
Get the min value of a given key.
at line 709
mixed
max(callable|int|string|null $callback = null)
Get the max value of a given key.
at line 714
Enumerable
nth(int $step, int $offset = 0)
Create a new collection consisting of every n-th element.
at line 721
Enumerable
only(mixed $keys)
Get the items with the specified keys.
at line 726
Enumerable
forPage(int $page, int $perPage)
"Paginate" the collection by slicing it into a smaller collection.
at line 734
Collection|Enumerable
partition(mixed $key, mixed $operator = null, mixed $value = null)
Partition the collection into two arrays using the given callback or key.
at line 744
Enumerable
concat(iterable $source)
Push all of the given items onto the collection.
at line 753
mixed
random(callable|int|string|null $number = null, bool $preserveKeys = false)
Get one or a specified number of items randomly from the collection.
at line 765
mixed
reduce(callable $callback, mixed $initial = null)
Reduce the collection to a single value.
at line 776
mixed
reduceInto(mixed $initial, callable $callback)
Reduce the collection to a single value by mutating an initial value.
at line 783
array
reduceSpread(callable $callback, mixed ...$initial)
Reduce the collection to multiple aggregate values.
at line 790
Enumerable
replace(mixed $items)
Replace the collection items with the given items.
at line 797
Enumerable
replaceRecursive(mixed $items)
Recursively replace the collection items with the given items.
at line 802
Enumerable
reverse()
Reverse items order.
at line 810
mixed
search(mixed $value, bool $strict = false)
Search the collection for a given value and return the corresponding key if successful.
at line 818
mixed
before(mixed $value, bool $strict = false)
Get the item before the given item.
at line 826
mixed
after(mixed $value, bool $strict = false)
Get the item after the given item.
at line 833
Enumerable
shuffle()
Shuffle the items in the collection.
at line 840
Collection|Enumerable
sliding(int $size = 2, int $step = 1)
Create chunks representing a "sliding window" view of the items in the collection.
at line 845
Enumerable
skip(int $count)
Skip the first {$count} items.
at line 852
Enumerable
skipUntil(mixed $value)
Skip items in the collection until the given condition is met.
at line 859
Enumerable
skipWhile(mixed $value)
Skip items in the collection while the given condition is met.
at line 864
Enumerable
slice(int $offset, int|null $length = null)
Get a slice of items from the enumerable.
at line 871
Enumerable
split(int $numberOfGroups)
Split a collection into a certain number of groups.
at line 882
mixed
sole(callable|string|null $key = null, mixed $operator = null, mixed $value = null)
Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
at line 892
mixed
firstOrFail(callable|string|null $key = null, mixed $operator = null, mixed $value = null)
Get the first item in the collection but throw an exception if no matching items exist.
at line 899
Enumerable
chunk(int $size)
Chunk the collection into chunks of the given size.
at line 907
Enumerable
chunkWhile(callable $callback)
Chunk the collection into chunks with a callback.
at line 915
Enumerable
chunkBy(callable|string $key)
Chunk the collection into chunks by comparing adjacent values using the given key or callback.
at line 922
Enumerable
splitIn(int $numberOfGroups)
Split a collection into a certain number of groups, and fill the first groups completely.
at line 929
Enumerable
sort(callable|int|null $callback = null)
Sort through each item with a callback.
at line 936
Enumerable
sortDesc(int $options = SORT_REGULAR)
Sort items in descending order.
at line 944
Enumerable
sortBy(array|callable|int|string $callback, int $options = SORT_REGULAR, SortDirection|bool $descending = false)
Sort the collection using the given callback.
at line 952
Enumerable
sortByDesc(array|callable|int|string $callback, int $options = SORT_REGULAR)
Sort the collection in descending order using the given callback.
at line 959
Enumerable
sortKeys(int $options = SORT_REGULAR, SortDirection|bool $descending = false)
Sort the collection keys.
at line 966
Enumerable
sortKeysDesc(int $options = SORT_REGULAR)
Sort the collection keys in descending order.
at line 973
Enumerable
sortKeysUsing(callable $callback)
Sort the collection keys using a callback.
at line 983
mixed
sum(callable|int|string|null $callback = null)
Get the sum of the given values.
at line 988
Enumerable
take(int $limit)
Take the first or last {$limit} items.
at line 995
Enumerable
takeUntil(mixed $value)
Take items in the collection until the given condition is met.
at line 1002
Enumerable
takeWhile(mixed $value)
Take items in the collection while the given condition is met.
at line 1010
Enumerable
tap(callable $callback)
Pass the collection to the given callback and then return it.
at line 1020
mixed
pipe(callable $callback)
Pass the enumerable to the given callback and return the result.
at line 1030
mixed
pipeInto(string $class)
Pass the collection into a new class.
at line 1037
mixed
pipeThrough(array $pipes)
Pass the collection through a series of callable pipes and return the result.
at line 1049
Collection|Enumerable
pluck(Closure|string|int|array|null $value, Closure|string|int|array|null $key = null)
Get the values of a given key.
at line 1056
Enumerable
reject(mixed $callback = true)
Create a collection of all elements that do not pass a given truth test.
at line 1061
Enumerable
dot(int|float $depth = INF)
Flatten a multi-dimensional associative array with dots.
at line 1066
Enumerable
undot()
Convert a flatten "dot" notation array into an expanded array.
at line 1073
Enumerable
unique(callable|int|string|null $key = null, bool $strict = false)
Return only unique items from the collection array.
at line 1080
Enumerable
uniqueStrict(callable|string|null $key = null)
Return only unique items from the collection array using strict comparison.
at line 1087
Enumerable
values()
Reset the keys on the underlying array.
at line 1097
Collection|Enumerable
pad(int $size, mixed $value)
Pad collection to the specified length with a value.
at line 1104
Traversable
getIterator()
Get the values iterator.
at line 1109
int
count()
Count the number of items in the collection.
at line 1117
Collection|Enumerable
countBy(callable|string|null $countBy = null)
Count the number of items in the collection by a field or using a callback.
at line 1130
Collection|Enumerable
zip(Arrayable|iterable ...$items)
Zip the collection together with one or more arrays.
e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); => [[1, 4], [2, 5], [3, 6]]
at line 1137
Collection
collect()
Collect the values into a collection.
at line 1149
mixed
jsonSerialize()
Convert the object into something JSON serializable.
at line 1163
string
toPrettyJson(int $options = 0)
Get the collection of items as pretty print formatted JSON.
at line 1168
CachingIterator
getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING)
Get a CachingIterator instance.
at line 1173
string
__toString()
Convert the collection to its string representation.
at line 1180
Enumerable
escapeWhenCastingToString(bool $escape = true)
Indicate that the model's string representation should be escaped when __toString is invoked.
at line 1188
static void
proxy(string $method)
Add a method to the list of proxied methods.
The method is registered on a worker-wide static and applies to every subsequent collection access; per-request use races across coroutines.
at line 1196
mixed
__get(string $key)
Dynamically access collection proxies.