Collection

class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable (View source)

Traits

Macroable

Properties

static protected array $macros The registered string macros. from Macroable
protected array $items The items contained in the collection.

Methods

static void macro(string $name, callable $macro)

Register a custom macro.

from Macroable
static bool hasMacro(string $name)

Checks if macro is registered.

from Macroable
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

from Macroable
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

from Macroable
void __construct(mixed $items = [])

Create a new collection.

static Collection make(mixed $items = [])

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

array all()

Get all of the items in the collection.

mixed avg(string|null $key = null)

Get the average value of a given key.

mixed average(string|null $key = null)

Alias for the "avg" method.

mixed|null median(null $key = null)

Get the median of a given key.

array mode(null $key = null)

Get the mode of a given key.

Collection collapse()

Collapse the collection of items into a single array.

bool contains(mixed $key, mixed $value = null)

Determine if an item exists in the collection.

Collection diff(mixed $items)

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

Collection diffKeys(mixed $items)

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

$this each(callable $callback)

Execute a callback over each item.

Collection every(int $step, int $offset)

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

Collection except(mixed $keys)

Get all items except for those with the specified keys.

Collection filter(callable $callback = null)

Run a filter over each of the items.

Collection where(string $key, mixed $value, bool $strict = true)

Filter items by the given key value pair.

Collection whereLoose(string $key, mixed $value)

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

Collection whereIn(string $key, array $values, bool $strict = true)

Filter items by the given key value pair.

Collection whereInLoose(string $key, array $values)

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

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

Get the first item from the collection.

Collection flatten(int $depth = INF)

Get a flattened array of the items in the collection.

Collection flip()

Flip the items in the collection.

$this forget(string|array $keys)

Remove an item from the collection by key.

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

Get an item from the collection by key.

Collection groupBy(callable|string $groupBy, bool $preserveKeys = false)

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

Collection keyBy(callable|string $keyBy)

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

bool has(mixed $key)

Determine if an item exists in the collection by key.

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

Concatenate values of a given key as a string.

Collection intersect(mixed $items)

Intersect the collection with the given items.

bool isEmpty()

Determine if the collection is empty or not.

bool useAsCallable(mixed $value)

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

Collection keys()

Get the keys of the collection items.

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

Get the last item from the collection.

Collection pluck(string $value, string|null $key = null)

Get the values of a given key.

Collection lists(string $value, string|null $key = null) deprecated

Alias for the "pluck" method.

Collection map(callable $callback)

Run a map over each of the items.

Collection flatMap(callable $callback)

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

mixed max(string|null $key = null)

Get the max value of a given key.

Collection merge(mixed $items)

Merge the collection with the given items.

Collection combine(mixed $values)

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

Collection union(mixed $items)

Union the collection with the given items.

mixed min(string|null $key = null)

Get the min value of a given key.

Collection only(mixed $keys)

Get the items with the specified keys.

Collection forPage(int $page, int $perPage)

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

mixed pipe(callable $callback)

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

mixed pop()

Get and remove the last item from the collection.

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

Push an item onto the beginning of the collection.

$this push(mixed $value)

Push an item onto the end of the collection.

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

Get and remove an item from the collection.

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

Put an item in the collection by key.

mixed random(int $amount = 1)

Get one or more items randomly from the collection.

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

Reduce the collection to a single value.

Collection reject(callable|mixed $callback)

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

Collection reverse()

Reverse items order.

mixed search(mixed $value, bool $strict = false)

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

mixed shift()

Get and remove the first item from the collection.

Collection shuffle(int $seed = null)

Shuffle the items in the collection.

Collection slice(int $offset, int $length = null)

Slice the underlying collection array.

Collection chunk(int $size)

Chunk the underlying collection array.

Collection sort(callable $callback = null)

Sort through each item with a callback.

Collection sortBy(callable|string $callback, int $options = SORT_REGULAR, bool $descending = false)

Sort the collection using the given callback.

Collection sortByDesc(callable|string $callback, int $options = SORT_REGULAR)

Sort the collection in descending order using the given callback.

Collection splice(int $offset, int|null $length = null, mixed $replacement = [])

Splice a portion of the underlying collection array.

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

Get the sum of the given values.

Collection take(int $limit)

Take the first or last {$limit} items.

$this transform(callable $callback)

Transform each item in the collection using a callback.

Collection unique(string|callable|null $key = null)

Return only unique items from the collection array.

Collection values()

Reset the keys on the underlying array.

callable valueRetriever(string $value)

Get a value retrieving callback.

Collection zip(mixed $items)

Zip the collection together with one or more arrays.

array toArray()

Get the collection of items as a plain array.

array jsonSerialize()

Convert the object into something JSON serializable.

string toJson(int $options)

Get the collection of items as JSON.

ArrayIterator getIterator()

Get an iterator for the items.

CachingIterator getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING)

Get a CachingIterator instance.

int count()

Count the number of items in the collection.

bool offsetExists(mixed $key)

Determine if an item exists at an offset.

mixed offsetGet(mixed $key)

Get an item at a given offset.

void offsetSet(mixed $key, mixed $value)

Set the item at a given offset.

void offsetUnset(string $key)

Unset the item at a given offset.

string __toString()

Convert the collection to its string representation.

array getArrayableItems(mixed $items)

Results array of items from Collection or Arrayable.

Details

static void macro(string $name, callable $macro)

Register a custom macro.

Parameters

string $name
callable $macro

Return Value

void

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

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

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

void __construct(mixed $items = [])

Create a new collection.

Parameters

mixed $items

Return Value

void

static Collection make(mixed $items = [])

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

Parameters

mixed $items

Return Value

Collection

array all()

Get all of the items in the collection.

Return Value

array

mixed avg(string|null $key = null)

Get the average value of a given key.

Parameters

string|null $key

Return Value

mixed

mixed average(string|null $key = null)

Alias for the "avg" method.

Parameters

string|null $key

Return Value

mixed

mixed|null median(null $key = null)

Get the median of a given key.

Parameters

null $key

Return Value

mixed|null

array mode(null $key = null)

Get the mode of a given key.

Parameters

null $key

Return Value

array

Collection collapse()

Collapse the collection of items into a single array.

Return Value

Collection

bool contains(mixed $key, mixed $value = null)

Determine if an item exists in the collection.

Parameters

mixed $key
mixed $value

Return Value

bool

Collection diff(mixed $items)

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

Parameters

mixed $items

Return Value

Collection

Collection diffKeys(mixed $items)

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

Parameters

mixed $items

Return Value

Collection

$this each(callable $callback)

Execute a callback over each item.

Parameters

callable $callback

Return Value

$this

Collection every(int $step, int $offset)

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

Parameters

int $step
int $offset

Return Value

Collection

Collection except(mixed $keys)

Get all items except for those with the specified keys.

Parameters

mixed $keys

Return Value

Collection

Collection filter(callable $callback = null)

Run a filter over each of the items.

Parameters

callable $callback

Return Value

Collection

Collection where(string $key, mixed $value, bool $strict = true)

Filter items by the given key value pair.

Parameters

string $key
mixed $value
bool $strict

Return Value

Collection

Collection whereLoose(string $key, mixed $value)

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

Parameters

string $key
mixed $value

Return Value

Collection

Collection whereIn(string $key, array $values, bool $strict = true)

Filter items by the given key value pair.

Parameters

string $key
array $values
bool $strict

Return Value

Collection

Collection whereInLoose(string $key, array $values)

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

Parameters

string $key
array $values

Return Value

Collection

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

Get the first item from the collection.

Parameters

callable $callback
mixed $default

Return Value

mixed

Collection flatten(int $depth = INF)

Get a flattened array of the items in the collection.

Parameters

int $depth

Return Value

Collection

Collection flip()

Flip the items in the collection.

Return Value

Collection

$this forget(string|array $keys)

Remove an item from the collection by key.

Parameters

string|array $keys

Return Value

$this

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

Get an item from the collection by key.

Parameters

mixed $key
mixed $default

Return Value

mixed

Collection groupBy(callable|string $groupBy, bool $preserveKeys = false)

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

Parameters

callable|string $groupBy
bool $preserveKeys

Return Value

Collection

Collection keyBy(callable|string $keyBy)

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

Parameters

callable|string $keyBy

Return Value

Collection

bool has(mixed $key)

Determine if an item exists in the collection by key.

Parameters

mixed $key

Return Value

bool

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

Concatenate values of a given key as a string.

Parameters

string $value
string $glue

Return Value

string

Collection intersect(mixed $items)

Intersect the collection with the given items.

Parameters

mixed $items

Return Value

Collection

bool isEmpty()

Determine if the collection is empty or not.

Return Value

bool

protected bool useAsCallable(mixed $value)

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

Parameters

mixed $value

Return Value

bool

Collection keys()

Get the keys of the collection items.

Return Value

Collection

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

Get the last item from the collection.

Parameters

callable $callback
mixed $default

Return Value

mixed

Collection pluck(string $value, string|null $key = null)

Get the values of a given key.

Parameters

string $value
string|null $key

Return Value

Collection

Collection lists(string $value, string|null $key = null) deprecated

deprecated

since version 5.2. Use the "pluck" method directly.

Alias for the "pluck" method.

Parameters

string $value
string|null $key

Return Value

Collection

Collection map(callable $callback)

Run a map over each of the items.

Parameters

callable $callback

Return Value

Collection

Collection flatMap(callable $callback)

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

Parameters

callable $callback

Return Value

Collection

mixed max(string|null $key = null)

Get the max value of a given key.

Parameters

string|null $key

Return Value

mixed

Collection merge(mixed $items)

Merge the collection with the given items.

Parameters

mixed $items

Return Value

Collection

Collection combine(mixed $values)

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

Parameters

mixed $values

Return Value

Collection

Collection union(mixed $items)

Union the collection with the given items.

Parameters

mixed $items

Return Value

Collection

mixed min(string|null $key = null)

Get the min value of a given key.

Parameters

string|null $key

Return Value

mixed

Collection only(mixed $keys)

Get the items with the specified keys.

Parameters

mixed $keys

Return Value

Collection

Collection forPage(int $page, int $perPage)

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

Parameters

int $page
int $perPage

Return Value

Collection

mixed pipe(callable $callback)

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

Parameters

callable $callback

Return Value

mixed

mixed pop()

Get and remove the last item from the collection.

Return Value

mixed

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

Push an item onto the beginning of the collection.

Parameters

mixed $value
mixed $key

Return Value

$this

$this push(mixed $value)

Push an item onto the end of the collection.

Parameters

mixed $value

Return Value

$this

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

Get and remove an item from the collection.

Parameters

mixed $key
mixed $default

Return Value

mixed

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

Put an item in the collection by key.

Parameters

mixed $key
mixed $value

Return Value

$this

mixed random(int $amount = 1)

Get one or more items randomly from the collection.

Parameters

int $amount

Return Value

mixed

Exceptions

InvalidArgumentException

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

Reduce the collection to a single value.

Parameters

callable $callback
mixed $initial

Return Value

mixed

Collection reject(callable|mixed $callback)

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

Parameters

callable|mixed $callback

Return Value

Collection

Collection reverse()

Reverse items order.

Return Value

Collection

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

Parameters

mixed $value
bool $strict

Return Value

mixed

mixed shift()

Get and remove the first item from the collection.

Return Value

mixed

Collection shuffle(int $seed = null)

Shuffle the items in the collection.

Parameters

int $seed

Return Value

Collection

Collection slice(int $offset, int $length = null)

Slice the underlying collection array.

Parameters

int $offset
int $length

Return Value

Collection

Collection chunk(int $size)

Chunk the underlying collection array.

Parameters

int $size

Return Value

Collection

Collection sort(callable $callback = null)

Sort through each item with a callback.

Parameters

callable $callback

Return Value

Collection

Collection sortBy(callable|string $callback, int $options = SORT_REGULAR, bool $descending = false)

Sort the collection using the given callback.

Parameters

callable|string $callback
int $options
bool $descending

Return Value

Collection

Collection sortByDesc(callable|string $callback, int $options = SORT_REGULAR)

Sort the collection in descending order using the given callback.

Parameters

callable|string $callback
int $options

Return Value

Collection

Collection splice(int $offset, int|null $length = null, mixed $replacement = [])

Splice a portion of the underlying collection array.

Parameters

int $offset
int|null $length
mixed $replacement

Return Value

Collection

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

Get the sum of the given values.

Parameters

callable|string|null $callback

Return Value

mixed

Collection take(int $limit)

Take the first or last {$limit} items.

Parameters

int $limit

Return Value

Collection

$this transform(callable $callback)

Transform each item in the collection using a callback.

Parameters

callable $callback

Return Value

$this

Collection unique(string|callable|null $key = null)

Return only unique items from the collection array.

Parameters

string|callable|null $key

Return Value

Collection

Collection values()

Reset the keys on the underlying array.

Return Value

Collection

protected callable valueRetriever(string $value)

Get a value retrieving callback.

Parameters

string $value

Return Value

callable

Collection zip(mixed $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]]

Parameters

mixed $items

Return Value

Collection

array toArray()

Get the collection of items as a plain array.

Return Value

array

array jsonSerialize()

Convert the object into something JSON serializable.

Return Value

array

string toJson(int $options)

Get the collection of items as JSON.

Parameters

int $options

Return Value

string

ArrayIterator getIterator()

Get an iterator for the items.

Return Value

ArrayIterator

CachingIterator getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING)

Get a CachingIterator instance.

Parameters

int $flags

Return Value

CachingIterator

int count()

Count the number of items in the collection.

Return Value

int

bool offsetExists(mixed $key)

Determine if an item exists at an offset.

Parameters

mixed $key

Return Value

bool

mixed offsetGet(mixed $key)

Get an item at a given offset.

Parameters

mixed $key

Return Value

mixed

void offsetSet(mixed $key, mixed $value)

Set the item at a given offset.

Parameters

mixed $key
mixed $value

Return Value

void

void offsetUnset(string $key)

Unset the item at a given offset.

Parameters

string $key

Return Value

void

string __toString()

Convert the collection to its string representation.

Return Value

string

protected array getArrayableItems(mixed $items)

Results array of items from Collection or Arrayable.

Parameters

mixed $items

Return Value

array

© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/5.2/Illuminate/Support/Collection.html