Properties

$items

$items : array

The items contained in the collection.

Type

array

$proxies

$proxies : array

The methods that can be proxied.

Type

array

$macros

$macros : array

The registered string macros.

Type

array

Methods

__construct()

__construct(mixed  $items = array()) 

Create a new collection.

Parameters

mixed $items

make()

make(mixed  $items = array()) : static

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

Parameters

mixed $items

Returns

static

wrap()

wrap(mixed  $value) : static

Wrap the given value in a collection if applicable.

Parameters

mixed $value

Returns

static

unwrap()

unwrap(array|static  $value) : array

Get the underlying items from the given collection if applicable.

Parameters

array|static $value

Returns

array

times()

times(integer  $number, callable  $callback = null) : static

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

Parameters

integer $number
callable $callback

Returns

static

all()

all() : array

Get all of the items in the collection.

Returns

array

avg()

avg(callable|string|null  $callback = null) : mixed

Get the average value of a given key.

Parameters

callable|string|null $callback

Returns

mixed

average()

average(callable|string|null  $callback = null) : mixed

Alias for the "avg" method.

Parameters

callable|string|null $callback

Returns

mixed

mode()

mode(mixed  $key = null) : array|null

Get the mode of a given key.

Parameters

mixed $key

Returns

array|null

collapse()

collapse() : static

Collapse the collection of items into a single array.

Returns

static

contains()

contains(mixed  $key, mixed  $operator = null, mixed  $value = null) : boolean

Determine if an item exists in the collection.

Parameters

mixed $key
mixed $operator
mixed $value

Returns

boolean

containsStrict()

containsStrict(mixed  $key, mixed  $value = null) : boolean

Determine if an item exists in the collection using strict comparison.

Parameters

mixed $key
mixed $value

Returns

boolean

crossJoin()

crossJoin(mixed  ...$lists) : static

Cross join with the given lists, returning all possible permutations.

Parameters

mixed $lists variadic

Returns

static

dd()

dd(  $args) : void

Dump the collection and end the script.

Parameters

$args

dump()

dump() : $this

Dump the collection.

Returns

$this

diff()

diff(mixed  $items) : static

Get the items in the collection that are not present in the given items.

Parameters

mixed $items

Returns

static

diffAssoc()

diffAssoc(mixed  $items) : static

Get the items in the collection whose keys and values are not present in the given items.

Parameters

mixed $items

Returns

static

diffKeys()

diffKeys(mixed  $items) : static

Get the items in the collection whose keys are not present in the given items.

Parameters

mixed $items

Returns

static

each()

each(callable  $callback) : $this

Execute a callback over each item.

Parameters

callable $callback

Returns

$this

eachSpread()

eachSpread(callable  $callback) : static

Execute a callback over each nested chunk of items.

Parameters

callable $callback

Returns

static

every()

every(string|callable  $key, mixed  $operator = null, mixed  $value = null) : boolean

Determine if all items in the collection pass the given test.

Parameters

string|callable $key
mixed $operator
mixed $value

Returns

boolean

except()

except(  $keys) : static

Get all items except for those with the specified keys.

Parameters

$keys

Returns

static

filter()

filter(callable|null  $callback = null) : static

Run a filter over each of the items.

Parameters

callable|null $callback

Returns

static

when()

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

Apply the callback if the value is truthy.

Parameters

boolean $value
callable $callback
callable $default

Returns

mixed

unless()

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

Apply the callback if the value is falsy.

Parameters

boolean $value
callable $callback
callable $default

Returns

mixed

where()

where(string  $key, mixed  $operator, mixed  $value = null) : static

Filter items by the given key value pair.

Parameters

string $key
mixed $operator
mixed $value

Returns

static

whereStrict()

whereStrict(string  $key, mixed  $value) : static

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

Parameters

string $key
mixed $value

Returns

static

whereIn()

whereIn(string  $key, mixed  $values, boolean  $strict = false) : static

Filter items by the given key value pair.

Parameters

string $key
mixed $values
boolean $strict

Returns

static

whereInStrict()

whereInStrict(string  $key, mixed  $values) : static

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

Parameters

string $key
mixed $values

Returns

static

whereNotIn()

whereNotIn(string  $key, mixed  $values, boolean  $strict = false) : static

Filter items by the given key value pair.

Parameters

string $key
mixed $values
boolean $strict

Returns

static

whereNotInStrict()

whereNotInStrict(string  $key, mixed  $values) : static

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

Parameters

string $key
mixed $values

Returns

static

first()

first(callable|null  $callback = null, mixed  $default = null) : mixed

Get the first item from the collection.

Parameters

callable|null $callback
mixed $default

Returns

mixed

firstWhere()

firstWhere(string  $key, mixed  $operator, mixed  $value = null) : static

Get the first item by the given key value pair.

Parameters

string $key
mixed $operator
mixed $value

Returns

static

flatten()

flatten(integer  $depth = INF) : static

Get a flattened array of the items in the collection.

Parameters

integer $depth

Returns

static

flip()

flip() : static

Flip the items in the collection.

Returns

static

forget()

forget(string|array  $keys) : $this

Remove an item from the collection by key.

Parameters

string|array $keys

Returns

$this

get()

get(mixed  $key, mixed  $default = null) : mixed

Get an item from the collection by key.

Parameters

mixed $key
mixed $default

Returns

mixed

groupBy()

groupBy(callable|string  $groupBy, boolean  $preserveKeys = false) : static

Group an associative array by a field or using a callback.

Parameters

callable|string $groupBy
boolean $preserveKeys

Returns

static

keyBy()

keyBy(callable|string  $keyBy) : static

Key an associative array by a field or using a callback.

Parameters

callable|string $keyBy

Returns

static

has()

has(mixed  $key) : boolean

Determine if an item exists in the collection by key.

Parameters

mixed $key

Returns

boolean

implode()

implode(string  $value, string  $glue = null) : string

Concatenate values of a given key as a string.

Parameters

string $value
string $glue

Returns

string

intersect()

intersect(mixed  $items) : static

Intersect the collection with the given items.

Parameters

mixed $items

Returns

static

intersectByKeys()

intersectByKeys(mixed  $items) : static

Intersect the collection with the given items by key.

Parameters

mixed $items

Returns

static

isEmpty()

isEmpty() : boolean

Determine if the collection is empty or not.

Returns

boolean

isNotEmpty()

isNotEmpty() : boolean

Determine if the collection is not empty.

Returns

boolean

keys()

keys() : static

Get the keys of the collection items.

Returns

static

last()

last(callable|null  $callback = null, mixed  $default = null) : mixed

Get the last item from the collection.

Parameters

callable|null $callback
mixed $default

Returns

mixed

pluck()

pluck(string|array  $value, string|null  $key = null) : static

Get the values of a given key.

Parameters

string|array $value
string|null $key

Returns

static

map()

map(callable  $callback) : static

Run a map over each of the items.

Parameters

callable $callback

Returns

static

mapSpread()

mapSpread(callable  $callback) : static

Run a map over each nested chunk of items.

Parameters

callable $callback

Returns

static

mapToDictionary()

mapToDictionary(callable  $callback) : static

Run a dictionary map over the items.

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

Parameters

callable $callback

Returns

static

mapToGroups()

mapToGroups(callable  $callback) : static

Run a grouping map over the items.

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

Parameters

callable $callback

Returns

static

mapWithKeys()

mapWithKeys(callable  $callback) : static

Run an associative map over each of the items.

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

Parameters

callable $callback

Returns

static

flatMap()

flatMap(callable  $callback) : static

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

Parameters

callable $callback

Returns

static

mapInto()

mapInto(string  $class) : static

Map the values into a new class.

Parameters

string $class

Returns

static

max()

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

Get the max value of a given key.

Parameters

callable|string|null $callback

Returns

mixed

merge()

merge(mixed  $items) : static

Merge the collection with the given items.

Parameters

mixed $items

Returns

static

combine()

combine(mixed  $values) : static

Create a collection by using this collection for keys and another for its values.

Parameters

mixed $values

Returns

static

union()

union(mixed  $items) : static

Union the collection with the given items.

Parameters

mixed $items

Returns

static

min()

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

Get the min value of a given key.

Parameters

callable|string|null $callback

Returns

mixed

nth()

nth(integer  $step, integer  $offset) : static

Create a new collection consisting of every n-th element.

Parameters

integer $step
integer $offset

Returns

static

only()

only(mixed  $keys) : static

Get the items with the specified keys.

Parameters

mixed $keys

Returns

static

forPage()

forPage(integer  $page, integer  $perPage) : static

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

Parameters

integer $page
integer $perPage

Returns

static

partition()

partition(callable|string  $key, mixed  $operator = null, mixed  $value = null) : static

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

Parameters

callable|string $key
mixed $operator
mixed $value

Returns

static

pipe()

pipe(callable  $callback) : mixed

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

Parameters

callable $callback

Returns

mixed

pop()

pop() : mixed

Get and remove the last item from the collection.

Returns

mixed

prepend()

prepend(mixed  $value, mixed  $key = null) : $this

Push an item onto the beginning of the collection.

Parameters

mixed $value
mixed $key

Returns

$this

push()

push(mixed  $value) : $this

Push an item onto the end of the collection.

Parameters

mixed $value

Returns

$this

concat()

concat(\Traversable  $source) : $this

Push all of the given items onto the collection.

Parameters

\Traversable $source

Returns

$this

pull()

pull(mixed  $key, mixed  $default = null) : mixed

Get and remove an item from the collection.

Parameters

mixed $key
mixed $default

Returns

mixed

put()

put(mixed  $key, mixed  $value) : $this

Put an item in the collection by key.

Parameters

mixed $key
mixed $value

Returns

$this

random()

random(integer|null  $number = null) : mixed

Get one or a specified number of items randomly from the collection.

Parameters

integer|null $number

Throws

\InvalidArgumentException

Returns

mixed

reduce()

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

Reduce the collection to a single value.

Parameters

callable $callback
mixed $initial

Returns

mixed

reject()

reject(callable|mixed  $callback) : static

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

Parameters

callable|mixed $callback

Returns

static

reverse()

reverse() : static

Reverse items order.

Returns

static

search()

search(mixed  $value, boolean  $strict = false) : mixed

Search the collection for a given value and return the corresponding key if successful.

Parameters

mixed $value
boolean $strict

Returns

mixed

shift()

shift() : mixed

Get and remove the first item from the collection.

Returns

mixed

shuffle()

shuffle(integer  $seed = null) : static

Shuffle the items in the collection.

Parameters

integer $seed

Returns

static

slice()

slice(integer  $offset, integer  $length = null) : static

Slice the underlying collection array.

Parameters

integer $offset
integer $length

Returns

static

split()

split(integer  $numberOfGroups) : static

Split a collection into a certain number of groups.

Parameters

integer $numberOfGroups

Returns

static

chunk()

chunk(integer  $size) : static

Chunk the underlying collection array.

Parameters

integer $size

Returns

static

sort()

sort(callable|null  $callback = null) : static

Sort through each item with a callback.

Parameters

callable|null $callback

Returns

static

sortBy()

sortBy(callable|string  $callback, integer  $options = SORT_REGULAR, boolean  $descending = false) : static

Sort the collection using the given callback.

Parameters

callable|string $callback
integer $options
boolean $descending

Returns

static

sortByDesc()

sortByDesc(callable|string  $callback, integer  $options = SORT_REGULAR) : static

Sort the collection in descending order using the given callback.

Parameters

callable|string $callback
integer $options

Returns

static

splice()

splice(integer  $offset, integer|null  $length = null, mixed  $replacement = array()) : static

Splice a portion of the underlying collection array.

Parameters

integer $offset
integer|null $length
mixed $replacement

Returns

static

sum()

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

Get the sum of the given values.

Parameters

callable|string|null $callback

Returns

mixed

take()

take(integer  $limit) : static

Take the first or last {$limit} items.

Parameters

integer $limit

Returns

static

tap()

tap(callable  $callback) : $this

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

Parameters

callable $callback

Returns

$this

transform()

transform(callable  $callback) : $this

Transform each item in the collection using a callback.

Parameters

callable $callback

Returns

$this

unique()

unique(string|callable|null  $key = null, boolean  $strict = false) : static

Return only unique items from the collection array.

Parameters

string|callable|null $key
boolean $strict

Returns

static

uniqueStrict()

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

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

Parameters

string|callable|null $key

Returns

static

values()

values() : static

Reset the keys on the underlying array.

Returns

static

zip()

zip(mixed  ...$items) : static

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]]

Parameters

mixed $items variadic

Returns

static

pad()

pad(integer  $size, mixed  $value) : static

Pad collection to the specified length with a value.

Parameters

integer $size
mixed $value

Returns

static

toArray()

toArray() : array

Get the collection of items as a plain array.

Returns

array

jsonSerialize()

jsonSerialize() : array

Convert the object into something JSON serializable.

Returns

array

toJson()

toJson(integer  $options) : string

Get the collection of items as JSON.

Parameters

integer $options

Returns

string

getIterator()

getIterator() : \ArrayIterator

Get an iterator for the items.

Returns

\ArrayIterator

getCachingIterator()

getCachingIterator(integer  $flags = \CachingIterator::CALL_TOSTRING) : \CachingIterator

Get a CachingIterator instance.

Parameters

integer $flags

Returns

\CachingIterator

count()

count() : integer

Count the number of items in the collection.

Returns

integer

offsetExists()

offsetExists(mixed  $key) : boolean

Determine if an item exists at an offset.

Parameters

mixed $key

Returns

boolean

offsetGet()

offsetGet(mixed  $key) : mixed

Get an item at a given offset.

Parameters

mixed $key

Returns

mixed

offsetSet()

offsetSet(mixed  $key, mixed  $value) : void

Set the item at a given offset.

Parameters

mixed $key
mixed $value

offsetUnset()

offsetUnset(string  $key) : void

Unset the item at a given offset.

Parameters

string $key

__toString()

__toString() : string

Convert the collection to its string representation.

Returns

string

proxy()

proxy(string  $method) : void

Add a method to the list of proxied methods.

Parameters

string $method

__get()

__get(string  $key) : mixed

Dynamically access collection proxies.

Parameters

string $key

Throws

\Exception

Returns

mixed

macro()

macro(string  $name, object|callable  $macro) : void

Register a custom macro.

Parameters

string $name
object|callable $macro

mixin()

mixin(object  $mixin) : void

Mix another object into the class.

Parameters

object $mixin

Throws

\ReflectionException

hasMacro()

hasMacro(string  $name) : boolean

Checks if macro is registered.

Parameters

string $name

Returns

boolean

__callStatic()

__callStatic(string  $method, array  $parameters) : mixed

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Throws

\BadMethodCallException

Returns

mixed

__call()

__call(string  $method, array  $parameters) : mixed

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Throws

\BadMethodCallException

Returns

mixed

operatorForWhere()

operatorForWhere(string  $key, string  $operator, mixed  $value = null) : \Closure

Get an operator checker callback.

Parameters

string $key
string $operator
mixed $value

Returns

\Closure

useAsCallable()

useAsCallable(mixed  $value) : boolean

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

Parameters

mixed $value

Returns

boolean

valueRetriever()

valueRetriever(string  $value) : callable

Get a value retrieving callback.

Parameters

string $value

Returns

callable

getArrayableItems()

getArrayableItems(mixed  $items) : array

Results array of items from Collection or Arrayable.

Parameters

mixed $items

Returns

array