class Illuminate / Support / Collection
You are viewing an older version of the documentation. For the latest, please visit master documentation.

Collection

Implements

Uses

Properties

protected

$items array

The items contained in the collection.

Default: []

Methods

public

__construct ( $items ) : void

Create a new collection.

Parameters

  • $items mixed
public static

range ( $from , $to ) : static

Create a collection with the given range.

Parameters

  • $from int
  • $to int
public

all ( ) : array

Get all of the items in the collection.

public

lazy ( ) : LazyCollection

Get a lazy collection for the items in this collection.

public

avg ( $callback ) : mixed

Get the average value of a given key.

Parameters

  • $callback callable|string|null
public

median ( $key ) : mixed

Get the median of a given key.

Parameters

  • $key string|array|null
public

mode ( $key ) : array|null

Get the mode of a given key.

Parameters

  • $key string|array|null
public

collapse ( ) : static

Collapse the collection of items into a single array.

public

contains ( $key , $operator , $value ) : bool

Determine if an item exists in the collection.

Parameters

  • $key mixed
  • $operator mixed
  • $value mixed
public

doesntContain ( $key , $operator , $value ) : bool

Determine if an item is not contained in the collection.

Parameters

  • $key mixed
  • $operator mixed
  • $value mixed
public

crossJoin ( $lists ) : static

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

Parameters

  • $lists mixed
public

diff ( $items ) : static

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

Parameters

  • $items mixed
public

diffUsing ( $items , callable $callback ) : static

Get the items in the collection that are not present in the given items, using the callback.

Parameters

  • $items mixed
  • $callback callable
public

diffAssoc ( $items ) : static

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

Parameters

  • $items mixed
public

diffAssocUsing ( $items , callable $callback ) : static

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

Parameters

  • $items mixed
  • $callback callable
public

diffKeys ( $items ) : static

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

Parameters

  • $items mixed
public

diffKeysUsing ( $items , callable $callback ) : static

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

Parameters

  • $items mixed
  • $callback callable
public

duplicates ( $callback , $strict ) : static

Retrieve duplicate items from the collection.

Parameters

  • $callback callable|string|null
  • $strict bool
public

duplicatesStrict ( $callback ) : static

Retrieve duplicate items from the collection using strict comparison.

Parameters

  • $callback callable|string|null
protected

duplicateComparator ( $strict ) : Closure

Get the comparison function to detect duplicates.

Parameters

  • $strict bool
public

except ( $keys ) : static

Get all items except for those with the specified keys.

Parameters

public

filter ( callable $callback ) : static

Run a filter over each of the items.

Parameters

  • $callback callable|null
public

first ( callable $callback , $default ) : mixed

Get the first item from the collection passing the given truth test.

Parameters

  • $callback callable|null
  • $default mixed
public

flatten ( $depth ) : static

Get a flattened array of the items in the collection.

Parameters

  • $depth int
public

flip ( ) : static

Flip the items in the collection.

public

forget ( $keys ) : $this

Remove an item from the collection by key.

Parameters

  • $keys string|int|array
public

get ( $key , $default ) : mixed

Get an item from the collection by key.

Parameters

  • $key mixed
  • $default mixed
public

getOrPut ( $key , $value ) : mixed

Get an item from the collection by key or add it to collection if it does not exist.

Parameters

  • $key mixed
  • $value mixed
public

groupBy ( $groupBy , $preserveKeys ) : static

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

Parameters

  • $groupBy array|callable|string
  • $preserveKeys bool
public

keyBy ( $keyBy ) : static

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

Parameters

  • $keyBy callable|string
public

has ( $key ) : bool

Determine if an item exists in the collection by key.

Parameters

  • $key mixed
public

hasAny ( $key ) : bool

Determine if any of the keys exist in the collection.

Parameters

  • $key mixed
public

implode ( $value , $glue ) : string

Concatenate values of a given key as a string.

Parameters

  • $value string
  • $glue string|null
public

intersect ( $items ) : static

Intersect the collection with the given items.

Parameters

  • $items mixed
public

intersectByKeys ( $items ) : static

Intersect the collection with the given items by key.

Parameters

  • $items mixed
public

isEmpty ( ) : bool

Determine if the collection is empty or not.

public

containsOneItem ( ) : bool

Determine if the collection contains a single item.

public

join ( $glue , $finalGlue ) : string

Join all items from the collection using a string. The final items can use a separate glue string.

Parameters

  • $glue string
  • $finalGlue string
public

keys ( ) : static

Get the keys of the collection items.

public

last ( callable $callback , $default ) : mixed

Get the last item from the collection.

Parameters

  • $callback callable|null
  • $default mixed
public

pluck ( $value , $key ) : static

Get the values of a given key.

Parameters

  • $value string|array|int|null
  • $key string|null
public

map ( callable $callback ) : static

Run a map over each of the items.

Parameters

  • $callback callable
public

mapToDictionary ( callable $callback ) : static

Run a dictionary map over the items.

Parameters

  • $callback callable
public

mapWithKeys ( callable $callback ) : static

Run an associative map over each of the items.

Parameters

  • $callback callable
public

merge ( $items ) : static

Merge the collection with the given items.

Parameters

  • $items mixed
public

mergeRecursive ( $items ) : static

Recursively merge the collection with the given items.

Parameters

  • $items mixed
public

combine ( $values ) : static

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

Parameters

  • $values mixed
public

union ( $items ) : static

Union the collection with the given items.

Parameters

  • $items mixed
public

nth ( $step , $offset ) : static

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

Parameters

  • $step int
  • $offset int
public

only ( $keys ) : static

Get the items with the specified keys.

Parameters

  • $keys mixed
public

pop ( $count ) : mixed

Get and remove the last N items from the collection.

Parameters

  • $count int
public

prepend ( $value , $key ) : $this

Push an item onto the beginning of the collection.

Parameters

  • $value mixed
  • $key mixed
public

push ( $values ) : $this

Push one or more items onto the end of the collection.

Parameters

  • $values mixed
public

concat ( $source ) : static

Push all of the given items onto the collection.

Parameters

  • $source iterable
public

pull ( $key , $default ) : mixed

Get and remove an item from the collection.

Parameters

  • $key mixed
  • $default mixed
public

put ( $key , $value ) : $this

Put an item in the collection by key.

Parameters

  • $key mixed
  • $value mixed
public

random ( $number ) : static|mixed

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

Parameters

  • $number int|null
public

replace ( $items ) : static

Replace the collection items with the given items.

Parameters

  • $items mixed
public

replaceRecursive ( $items ) : static

Recursively replace the collection items with the given items.

Parameters

  • $items mixed
public

reverse ( ) : static

Reverse items order.

public

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

Parameters

  • $value mixed
  • $strict bool
public

shift ( $count ) : mixed

Get and remove the first N items from the collection.

Parameters

  • $count int
public

shuffle ( $seed ) : static

Shuffle the items in the collection.

Parameters

  • $seed int|null
public

sliding ( $size , $step ) : static

Create chunks representing a "sliding window" view of the items in the collection.

Parameters

  • $size int
  • $step int
public

skip ( $count ) : static

Skip the first {$count} items.

Parameters

  • $count int
public

skipUntil ( $value ) : static

Skip items in the collection until the given condition is met.

Parameters

  • $value mixed
public

skipWhile ( $value ) : static

Skip items in the collection while the given condition is met.

Parameters

  • $value mixed
public

slice ( $offset , $length ) : static

Slice the underlying collection array.

Parameters

  • $offset int
  • $length int|null
public

split ( $numberOfGroups ) : static

Split a collection into a certain number of groups.

Parameters

  • $numberOfGroups int
public

splitIn ( $numberOfGroups ) : static

Split a collection into a certain number of groups, and fill the first groups completely.

Parameters

  • $numberOfGroups int
public

sole ( $key , $operator , $value ) : mixed

Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.

Parameters

  • $key mixed
  • $operator mixed
  • $value mixed
public

firstOrFail ( $key , $operator , $value ) : mixed

Get the first item in the collection but throw an exception if no matching items exist.

Parameters

  • $key mixed
  • $operator mixed
  • $value mixed
public

chunk ( $size ) : static

Chunk the collection into chunks of the given size.

Parameters

  • $size int
public

chunkWhile ( callable $callback ) : static

Chunk the collection into chunks with a callback.

Parameters

  • $callback callable
public

sort ( $callback ) : static

Sort through each item with a callback.

Parameters

  • $callback callable|int|null
public

sortDesc ( $options ) : static

Sort items in descending order.

Parameters

  • $options int
public

sortBy ( $callback , $options , $descending ) : static

Sort the collection using the given callback.

Parameters

  • $callback callable|array|string
  • $options int
  • $descending bool
protected

sortByMany ( array $comparisons ) : static

Sort the collection using multiple comparisons.

Parameters

  • $comparisons array
public

sortByDesc ( $callback , $options ) : static

Sort the collection in descending order using the given callback.

Parameters

  • $callback callable|string
  • $options int
public

sortKeys ( $options , $descending ) : static

Sort the collection keys.

Parameters

  • $options int
  • $descending bool
public

sortKeysDesc ( $options ) : static

Sort the collection keys in descending order.

Parameters

  • $options int
public

sortKeysUsing ( callable $callback ) : static

Sort the collection keys using a callback.

Parameters

  • $callback callable
public

splice ( $offset , $length , $replacement ) : static

Splice a portion of the underlying collection array.

Parameters

  • $offset int
  • $length int|null
  • $replacement mixed
public

take ( $limit ) : static

Take the first or last {$limit} items.

Parameters

  • $limit int
public

takeUntil ( $value ) : static

Take items in the collection until the given condition is met.

Parameters

  • $value mixed
public

takeWhile ( $value ) : static

Take items in the collection while the given condition is met.

Parameters

  • $value mixed
public

transform ( callable $callback ) : $this

Transform each item in the collection using a callback.

Parameters

  • $callback callable
public

undot ( ) : static

Convert a flatten "dot" notation array into an expanded array.

public

unique ( $key , $strict ) : static

Return only unique items from the collection array.

Parameters

  • $key string|callable|null
  • $strict bool
public

values ( ) : static

Reset the keys on the underlying array.

public

zip ( $items ) : static

Zip the collection together with one or more arrays.

Parameters

  • $items mixed
public

pad ( $size , $value ) : static

Pad collection to the specified length with a value.

Parameters

  • $size int
  • $value mixed
public

getIterator ( ) : ArrayIterator

Get an iterator for the items.

public

count ( ) : int

Count the number of items in the collection.

public

countBy ( $countBy ) : static

Count the number of items in the collection by a field or using a callback.

Parameters

  • $countBy callable|string
public

add ( $item ) : $this

Add an item to the collection.

Parameters

  • $item mixed
public

toBase ( ) : Collection

Get a base Support collection instance from this collection.

public

offsetExists ( $key ) : bool

Determine if an item exists at an offset.

Parameters

  • $key mixed
public

offsetGet ( $key ) : mixed

Get an item at a given offset.

Parameters

  • $key mixed
public

offsetSet ( $key , $value ) : void

Set the item at a given offset.

Parameters

  • $key mixed
  • $value mixed
public

offsetUnset ( $key ) : void

Unset the item at a given offset.

Parameters

  • $key mixed