trait EnumeratesValues

Traits

Constants

protected DEFAULT_PROXIES

The default methods that can be proxied.

Properties

protected bool $escapeWhenCastingToString

Indicates that the object's string representation should be escaped when __toString is invoked.

static protected array<int, string> $proxies

The methods that can be proxied.

TValue, static> read-only $average
TValue, static> read-only $avg
TValue, static> read-only $contains
TValue, static> read-only $doesntContain
TValue, static> read-only $each
TValue, static> read-only $every
TValue, static> read-only $filter
TValue, static> read-only $first
TValue, static> read-only $flatMap
TValue, static> read-only $groupBy
TValue, static> read-only $hasMany
TValue, static> read-only $hasSole
TValue, static> read-only $keyBy
TValue, static> read-only $last
TValue, static> read-only $map
TValue, static> read-only $max
TValue, static> read-only $min
TValue, static> read-only $partition
TValue, static> read-only $percentage
TValue, static> read-only $reject
TValue, static> read-only $skipUntil
TValue, static> read-only $skipWhile
TValue, static> read-only $sole
TValue, static> read-only $some
TValue, static> read-only $sortBy
TValue, static> read-only $sortByDesc
TValue, static> read-only $sum
TValue, static> read-only $takeUntil
TValue, static> read-only $takeWhile
TValue, static> read-only $unique
TValue, static> read-only $unless
TValue, static> read-only $until
TValue, static> read-only $when

Methods

mixed
when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

mixed
unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

make(mixed $items = [], mixed ...$args)

Create a new collection instance if the value isn't one already.

wrap(mixed $value, mixed ...$args)

Wrap the given value in a collection if applicable.

static mixed
unwrap(mixed $value)

Get the underlying items from the given collection if applicable.

empty(mixed ...$args)

Create a new instance with no items.

times(int $number, callable|null $callback = null, mixed ...$args)

Create a new collection by invoking the callback a given amount of times.

fromJson(string $json, int $depth = 513, int $flags = 0, mixed ...$args)

Create a new collection by decoding a JSON string.

float|int|null
avg(callable|int|string|null $callback = null)

Get the average value of a given key.

float|int|null
average(callable|string|null $callback = null)

Alias for the "avg" method.

bool
some(mixed $key, mixed $operator = null, mixed $value = null)

Alias for the "contains" method.

never
dd(mixed ...$args)

Dump the given arguments and terminate execution.

dump(mixed ...$args)

Dump the items.

each(callable $callback)

Execute a callback over each item.

eachSpread(callable $callback)

Execute a callback over each nested chunk of items.

bool
every(mixed $key, mixed $operator = null, mixed $value = null)

Determine if all items pass the given truth test.

mixed
firstWhere(callable|string $key, mixed $operator = null, mixed $value = null)

Get the first item by the given key value pair.

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.

mixed
value(string $key, mixed $default = null)

Get a single key's value from the first matching item in the collection.

ensure(string|array $type)

Ensure that every item in the collection is of the expected type.

bool
isNotEmpty()

Determine if the collection is not empty.

mapSpread(callable $callback)

Run a map over each nested chunk of items.

mapToGroups(callable $callback)

Run a grouping map over the items.

flatMap(callable $callback)

Map a collection and flatten the result by a single level.

mapInto(string $class)

Map the values into a new class.

mixed
min(callable|int|string|null $callback = null)

Get the min value of a given key.

mixed
max(callable|int|string|null $callback = null)

Get the max value of a given key.

forPage(int $page, int $perPage)

"Paginate" the collection by slicing it into a smaller collection.

partition(mixed $key, mixed $operator = null, mixed $value = null)

Partition the collection into two arrays using the given callback or key.

float|null
percentage(callable $callback, int $precision = 2)

Calculate the percentage of items that pass a given truth test.

mixed
sum(callable|int|string|null $callback = null)

Get the sum of the given values.

mixed
whenEmpty(callable $callback, callable|null $default = null)

Apply the callback if the collection is empty.

mixed
whenNotEmpty(callable $callback, callable|null $default = null)

Apply the callback if the collection is not empty.

mixed
unlessEmpty(callable $callback, callable|null $default = null)

Apply the callback unless the collection is empty.

mixed
unlessNotEmpty(callable $callback, callable|null $default = null)

Apply the callback unless the collection is not empty.

where(callable|string|null $key, mixed $operator = null, mixed $value = null)

Filter items by the given key value pair.

whereNull(string|null $key = null)

Filter items where the value for the given key is null.

whereNotNull(string|null $key = null)

Filter items where the value for the given key is not null.

whereStrict(callable|string|null $key, mixed $value)

Filter items by the given key value pair using strict comparison.

whereIn(string $key, Arrayable|iterable $values, bool $strict = false)

Filter items by the given key value pair.

whereInStrict(string $key, Arrayable|iterable $values)

Filter items by the given key value pair using strict comparison.

whereBetween(string $key, Arrayable|iterable $values)

Filter items such that the value of the given key is between the given values.

whereNotBetween(string $key, Arrayable|iterable $values)

Filter items such that the value of the given key is not between the given values.

whereNotIn(string $key, Arrayable|iterable $values, bool $strict = false)

Filter items by the given key value pair.

whereNotInStrict(string $key, Arrayable|iterable $values)

Filter items by the given key value pair using strict comparison.

whereInstanceOf(string|array $type)

Filter the items, removing any items that don't match the given type(s).

mixed
pipe(callable $callback)

Pass the collection to the given callback and return the result.

mixed
pipeInto(string $class)

Pass the collection into a new class.

mixed
pipeThrough(array $callbacks)

Pass the collection through a series of callable pipes and return the result.

mixed
reduce(callable $callback, mixed $initial = null)

Reduce the collection to a single value.

mixed
reduceInto(mixed $initial, callable $callback)

Reduce the collection to a single value by mutating an initial value.

array
reduceSpread(callable $callback, mixed ...$initial)

Reduce the collection to multiple aggregate values.

mixed
reduceWithKeys(callable $callback, mixed $initial = null)

Reduce an associative collection to a single value.

reject(mixed $callback = true)

Create a collection of all elements that do not pass a given truth test.

chunkBy(callable|string $key)

Chunk the collection into chunks by comparing adjacent values using the given key or callback.

tap(callable $callback)

Pass the collection to the given callback and then return it.

unique(callable|int|string|null $key = null, bool $strict = false)

Return only unique items from the collection array.

uniqueStrict(callable|string|null $key = null)

Return only unique items from the collection array using strict comparison.

collect()

Collect the values into a collection.

array
toArray()

Get the collection of items as a plain array.

array
jsonSerialize()

Convert the object into something JSON serializable.

string
toJson(int $options = 0)

Get the collection of items as JSON.

string
toPrettyJson(int $options = 0)

Get the collection of items as pretty print formatted JSON.

getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING)

Get a CachingIterator instance.

string
__toString()

Convert the collection to its string representation.

escapeWhenCastingToString(bool $escape = true)

Indicate that the model's string representation should be escaped when __toString is invoked.

static void
proxy(string $method)

Add a method to the list of proxied methods.

static void
flushProxies()

Flush the registered proxies, restoring the default list.

mixed
__get(string $key)

Dynamically access collection proxies.

array
getArrayableItems(mixed $items)

Results array of items from Collection or Arrayable.

callable
operatorForWhere(callable|string|null $key, mixed $operator = null, mixed $value = null)

Get an operator checker callback.

bool
useAsCallable(mixed $value)

Determine if the given value is callable, but not a string.

callable
valueRetriever(callable|int|string|null $value)

Get a value retrieving callback.

equality(mixed $value)

Make a function to check an item's equality.

negate(Closure $callback)

Make a function using another function, by negating its result.

identity()

Make a function that returns what's passed to it.

Details

in Conditionable at line 23
mixed when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Conditionable at line 56
mixed unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

at line 129
static EnumeratesValues make(mixed $items = [], mixed ...$args)

Create a new collection instance if the value isn't one already.

Parameters

mixed $items
mixed ...$args

Return Value

EnumeratesValues

at line 142
static EnumeratesValues wrap(mixed $value, mixed ...$args)

Wrap the given value in a collection if applicable.

Parameters

mixed $value
mixed ...$args

Return Value

EnumeratesValues

at line 159
static mixed unwrap(mixed $value)

Get the underlying items from the given collection if applicable.

Parameters

mixed $value

Return Value

mixed

at line 167
static EnumeratesValues empty(mixed ...$args)

Create a new instance with no items.

Parameters

mixed ...$args

Return Value

EnumeratesValues

at line 180
static Collection|EnumeratesValues times(int $number, callable|null $callback = null, mixed ...$args)

Create a new collection by invoking the callback a given amount of times.

Parameters

int $number
callable|null $callback
mixed ...$args

Return Value

Collection|EnumeratesValues

at line 196
static EnumeratesValues fromJson(string $json, int $depth = 513, int $flags = 0, mixed ...$args)

Create a new collection by decoding a JSON string.

Parameters

string $json
int $depth
int $flags
mixed ...$args

Return Value

EnumeratesValues

at line 207
float|int|null avg(callable|int|string|null $callback = null)

Get the average value of a given key.

Parameters

callable|int|string|null $callback

Return Value

float|int|null

at line 228
float|int|null average(callable|string|null $callback = null)

Alias for the "avg" method.

Parameters

callable|string|null $callback

Return Value

float|int|null

at line 238
bool some(mixed $key, mixed $operator = null, mixed $value = null)

Alias for the "contains" method.

Parameters

mixed $key
mixed $operator
mixed $value

Return Value

bool

at line 246
never dd(mixed ...$args)

Dump the given arguments and terminate execution.

Parameters

mixed ...$args

Return Value

never

at line 254
EnumeratesValues dump(mixed ...$args)

Dump the items.

Parameters

mixed ...$args

Return Value

EnumeratesValues

at line 266
EnumeratesValues each(callable $callback)

Execute a callback over each item.

Parameters

callable $callback

Return Value

EnumeratesValues

at line 280
EnumeratesValues eachSpread(callable $callback)

Execute a callback over each nested chunk of items.

Parameters

callable $callback

Return Value

EnumeratesValues

at line 292
bool every(mixed $key, mixed $operator = null, mixed $value = null)

Determine if all items pass the given truth test.

Parameters

mixed $key
mixed $operator
mixed $value

Return Value

bool

at line 314
mixed firstWhere(callable|string $key, mixed $operator = null, mixed $value = null)

Get the first item by the given key value pair.

Parameters

callable|string $key
mixed $operator
mixed $value

Return Value

mixed

at line 324
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.

Parameters

callable|string|null $key
mixed $operator
mixed $value

Return Value

bool

at line 345
mixed value(string $key, mixed $default = null)

Get a single key's value from the first matching item in the collection.

Parameters

string $key
mixed $default

Return Value

mixed

at line 364
EnumeratesValues ensure(string|array $type)

Ensure that every item in the collection is of the expected type.

Parameters

string|array $type

Return Value

EnumeratesValues

Exceptions

UnexpectedValueException

at line 393
bool isNotEmpty()

Determine if the collection is not empty.

Return Value

bool

at line 403
Collection|EnumeratesValues mapSpread(callable $callback)

Run a map over each nested chunk of items.

Parameters

callable $callback

Return Value

Collection|EnumeratesValues

at line 421
Collection|EnumeratesValues mapToGroups(callable $callback)

Run a grouping map over the items.

The callback should return an associative array with a single key/value pair.

Parameters

callable $callback

Return Value

Collection|EnumeratesValues

at line 437
Collection|EnumeratesValues flatMap(callable $callback)

Map a collection and flatten the result by a single level.

Parameters

callable $callback

Return Value

Collection|EnumeratesValues

at line 450
Collection|EnumeratesValues mapInto(string $class)

Map the values into a new class.

Parameters

string $class

Return Value

Collection|EnumeratesValues

at line 467
mixed min(callable|int|string|null $callback = null)

Get the min value of a given key.

Parameters

callable|int|string|null $callback

Return Value

mixed

at line 484
mixed max(callable|int|string|null $callback = null)

Get the max value of a given key.

Parameters

callable|int|string|null $callback

Return Value

mixed

at line 498
EnumeratesValues forPage(int $page, int $perPage)

"Paginate" the collection by slicing it into a smaller collection.

Parameters

int $page
int $perPage

Return Value

EnumeratesValues

at line 511
Collection|EnumeratesValues partition(mixed $key, mixed $operator = null, mixed $value = null)

Partition the collection into two arrays using the given callback or key.

Parameters

mixed $key
mixed $operator
mixed $value

Return Value

Collection|EnumeratesValues

at line 528
float|null percentage(callable $callback, int $precision = 2)

Calculate the percentage of items that pass a given truth test.

Parameters

callable $callback
int $precision

Return Value

float|null

at line 548
mixed sum(callable|int|string|null $callback = null)

Get the sum of the given values.

Parameters

callable|int|string|null $callback

Return Value

mixed

at line 566
mixed whenEmpty(callable $callback, callable|null $default = null)

Apply the callback if the collection is empty.

Parameters

callable $callback
callable|null $default

Return Value

mixed

at line 580
mixed whenNotEmpty(callable $callback, callable|null $default = null)

Apply the callback if the collection is not empty.

Parameters

callable $callback
callable|null $default

Return Value

mixed

at line 594
mixed unlessEmpty(callable $callback, callable|null $default = null)

Apply the callback unless the collection is empty.

Parameters

callable $callback
callable|null $default

Return Value

mixed

at line 608
mixed unlessNotEmpty(callable $callback, callable|null $default = null)

Apply the callback unless the collection is not empty.

Parameters

callable $callback
callable|null $default

Return Value

mixed

at line 616
EnumeratesValues where(callable|string|null $key, mixed $operator = null, mixed $value = null)

Filter items by the given key value pair.

Parameters

callable|string|null $key
mixed $operator
mixed $value

Return Value

EnumeratesValues

at line 624
EnumeratesValues whereNull(string|null $key = null)

Filter items where the value for the given key is null.

Parameters

string|null $key

Return Value

EnumeratesValues

at line 632
EnumeratesValues whereNotNull(string|null $key = null)

Filter items where the value for the given key is not null.

Parameters

string|null $key

Return Value

EnumeratesValues

at line 640
EnumeratesValues whereStrict(callable|string|null $key, mixed $value)

Filter items by the given key value pair using strict comparison.

Parameters

callable|string|null $key
mixed $value

Return Value

EnumeratesValues

at line 648
EnumeratesValues whereIn(string $key, Arrayable|iterable $values, bool $strict = false)

Filter items by the given key value pair.

Parameters

string $key
Arrayable|iterable $values
bool $strict

Return Value

EnumeratesValues

at line 658
EnumeratesValues whereInStrict(string $key, Arrayable|iterable $values)

Filter items by the given key value pair using strict comparison.

Parameters

string $key
Arrayable|iterable $values

Return Value

EnumeratesValues

at line 666
EnumeratesValues whereBetween(string $key, Arrayable|iterable $values)

Filter items such that the value of the given key is between the given values.

Parameters

string $key
Arrayable|iterable $values

Return Value

EnumeratesValues

at line 676
EnumeratesValues whereNotBetween(string $key, Arrayable|iterable $values)

Filter items such that the value of the given key is not between the given values.

Parameters

string $key
Arrayable|iterable $values

Return Value

EnumeratesValues

at line 692
EnumeratesValues whereNotIn(string $key, Arrayable|iterable $values, bool $strict = false)

Filter items by the given key value pair.

Parameters

string $key
Arrayable|iterable $values
bool $strict

Return Value

EnumeratesValues

at line 702
EnumeratesValues whereNotInStrict(string $key, Arrayable|iterable $values)

Filter items by the given key value pair using strict comparison.

Parameters

string $key
Arrayable|iterable $values

Return Value

EnumeratesValues

at line 715
EnumeratesValues whereInstanceOf(string|array $type)

Filter the items, removing any items that don't match the given type(s).

Parameters

string|array $type

Return Value

EnumeratesValues

at line 742
mixed pipe(callable $callback)

Pass the collection to the given callback and return the result.

Parameters

callable $callback

Return Value

mixed

at line 755
mixed pipeInto(string $class)

Pass the collection into a new class.

Parameters

string $class

Return Value

mixed

at line 765
mixed pipeThrough(array $callbacks)

Pass the collection through a series of callable pipes and return the result.

Parameters

array $callbacks

Return Value

mixed

at line 783
mixed reduce(callable $callback, mixed $initial = null)

Reduce the collection to a single value.

Parameters

callable $callback
mixed $initial

Return Value

mixed

at line 803
mixed reduceInto(mixed $initial, callable $callback)

Reduce the collection to a single value by mutating an initial value.

Parameters

mixed $initial
callable $callback

Return Value

mixed

at line 817
array reduceSpread(callable $callback, mixed ...$initial)

Reduce the collection to multiple aggregate values.

Parameters

callable $callback
mixed ...$initial

Return Value

array

Exceptions

UnexpectedValueException

at line 846
mixed reduceWithKeys(callable $callback, mixed $initial = null)

Reduce an associative collection to a single value.

Parameters

callable $callback
mixed $initial

Return Value

mixed

at line 856
EnumeratesValues reject(mixed $callback = true)

Create a collection of all elements that do not pass a given truth test.

Parameters

mixed $callback

Return Value

EnumeratesValues

at line 873
EnumeratesValues chunkBy(callable|string $key)

Chunk the collection into chunks by comparing adjacent values using the given key or callback.

Parameters

callable|string $key

Return Value

EnumeratesValues

at line 888
EnumeratesValues tap(callable $callback)

Pass the collection to the given callback and then return it.

Parameters

callable $callback

Return Value

EnumeratesValues

at line 900
EnumeratesValues unique(callable|int|string|null $key = null, bool $strict = false)

Return only unique items from the collection array.

Parameters

callable|int|string|null $key
bool $strict

Return Value

EnumeratesValues

at line 920
EnumeratesValues uniqueStrict(callable|string|null $key = null)

Return only unique items from the collection array using strict comparison.

Parameters

callable|string|null $key

Return Value

EnumeratesValues

at line 930
Collection collect()

Collect the values into a collection.

Return Value

Collection

at line 940
array toArray()

Get the collection of items as a plain array.

Return Value

array

at line 950
array jsonSerialize()

Convert the object into something JSON serializable.

Return Value

array

at line 968
string toJson(int $options = 0)

Get the collection of items as JSON.

Parameters

int $options

Return Value

string

Exceptions

JsonException

at line 978
string toPrettyJson(int $options = 0)

Get the collection of items as pretty print formatted JSON.

Parameters

int $options

Return Value

string

Exceptions

JsonException

at line 986
CachingIterator getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING)

Get a CachingIterator instance.

Parameters

int $flags

Return Value

CachingIterator

at line 995
string __toString()

Convert the collection to its string representation.

Return Value

string

at line 1005
EnumeratesValues escapeWhenCastingToString(bool $escape = true)

Indicate that the model's string representation should be escaped when __toString is invoked.

Parameters

bool $escape

Return Value

EnumeratesValues

at line 1019
static void proxy(string $method)

Add a method to the list of proxied methods.

Boot or tests only. The method is registered on a worker-wide static and applies to every subsequent collection access; per-request use races across coroutines.

Parameters

string $method

Return Value

void

at line 1030
static void flushProxies()

Flush the registered proxies, restoring the default list.

Boot or tests only. Resets the worker-wide proxy registry; concurrent coroutines may resolve different proxies depending on timing.

Return Value

void

at line 1040
mixed __get(string $key)

Dynamically access collection proxies.

Parameters

string $key

Return Value

mixed

Exceptions

Exception

at line 1054
protected array getArrayableItems(mixed $items)

Results array of items from Collection or Arrayable.

Parameters

mixed $items

Return Value

array

at line 1064
protected callable operatorForWhere(callable|string|null $key, mixed $operator = null, mixed $value = null)

Get an operator checker callback.

Parameters

callable|string|null $key
mixed $operator
mixed $value

Return Value

callable

at line 1118
protected bool useAsCallable(mixed $value)

Determine if the given value is callable, but not a string.

Parameters

mixed $value

Return Value

bool

at line 1126
protected callable valueRetriever(callable|int|string|null $value)

Get a value retrieving callback.

Parameters

callable|int|string|null $value

Return Value

callable

at line 1140
protected Closure equality(mixed $value)

Make a function to check an item's equality.

Parameters

mixed $value

Return Value

Closure

at line 1148
protected Closure negate(Closure $callback)

Make a function using another function, by negating its result.

Parameters

Closure $callback

Return Value

Closure

at line 1158
protected Closure identity()

Make a function that returns what's passed to it.

Return Value

Closure