PHP Class Illuminate\Support\Collection

Inheritance: implements ArrayAcces\ArrayAccess, implements Illuminate\Support\Contracts\ArrayableInterface, implements Countabl\Countable, implements IteratorAggregat\IteratorAggregate, implements Illuminate\Support\Contracts\JsonableInterface
Exibir arquivo Open project: tightenco/collect Class Usage Examples

Protected Properties

Property Type Description
$items array The items contained in the collection.

Public Methods

Method Description
__construct ( mixed $items = [] ) : void Create a new collection.
__toString ( ) : string Convert the collection to its string representation.
all ( ) : array Get all of the items in the collection.
average ( callable | string | null $callback = null ) : mixed Alias for the "avg" method.
avg ( callable | string | null $callback = null ) : mixed Get the average value of a given key.
chunk ( integer $size ) : static Chunk the underlying collection array.
collapse ( ) : static Collapse the collection of items into a single array.
combine ( mixed $values ) : static Create a collection by using this collection for keys and another for its values.
contains ( mixed $key, mixed $value = null ) : boolean Determine if an item exists in the collection.
containsStrict ( mixed $key, mixed $value = null ) : boolean Determine if an item exists in the collection using strict comparison.
count ( ) : integer Count the number of items in the collection.
diff ( mixed $items ) : static Get the items in the collection that are not present in the given items.
diffKeys ( mixed $items ) : static Get the items in the collection whose keys are not present in the given items.
each ( callable $callback ) Execute a callback over each item.
every ( integer $step, integer $offset ) : static Create a new collection consisting of every n-th element.
except ( mixed $keys ) : static Get all items except for those with the specified keys.
filter ( callable $callback = null ) : static Run a filter over each of the items.
first ( callable $callback = null, mixed $default = null ) : mixed Get the first item from the collection.
flatMap ( callable $callback ) : static Map a collection and flatten the result by a single level.
flatten ( integer $depth = INF ) : static Get a flattened array of the items in the collection.
flip ( ) : static Flip the items in the collection.
forPage ( integer $page, integer $perPage ) : static "Paginate" the collection by slicing it into a smaller collection.
forget ( string | array $keys ) Remove an item from the collection by key.
get ( mixed $key, mixed $default = null ) : mixed Get an item from the collection by key.
getCachingIterator ( integer $flags = CachingIterator::CALL_TOSTRING ) : CachingIterator Get a CachingIterator instance.
getIterator ( ) : ArrayIterator Get an iterator for the items.
groupBy ( callable | string $groupBy, boolean $preserveKeys = false ) : static Group an associative array by a field or using a callback.
has ( mixed $key ) : boolean Determine if an item exists in the collection by key.
implode ( string $value, string $glue = null ) : string Concatenate values of a given key as a string.
intersect ( mixed $items ) : static Intersect the collection with the given items.
isEmpty ( ) : boolean Determine if the collection is empty or not.
jsonSerialize ( ) : array Convert the object into something JSON serializable.
keyBy ( callable | string $keyBy ) : static Key an associative array by a field or using a callback.
keys ( ) : static Get the keys of the collection items.
last ( callable $callback = null, mixed $default = null ) : mixed Get the last item from the collection.
make ( mixed $items = [] ) : static Create a new collection instance if the value isn't one already.
map ( callable $callback ) : static Run a map over each of the items.
mapWithKeys ( callable $callback ) : static Run an associative map over each of the items.
max ( callable | string | null $callback = null ) : mixed Get the max value of a given key.
median ( null $key = null ) : mixed | null Get the median of a given key.
merge ( mixed $items ) : static Merge the collection with the given items.
min ( callable | string | null $callback = null ) : mixed Get the min value of a given key.
mode ( null $key = null ) : array Get the mode of a given key.
offsetExists ( mixed $key ) : boolean Determine if an item exists at an offset.
offsetGet ( mixed $key ) : mixed Get an item at a given offset.
offsetSet ( mixed $key, mixed $value ) : void Set the item at a given offset.
offsetUnset ( string $key ) : void Unset the item at a given offset.
only ( mixed $keys ) : static Get the items with the specified keys.
pipe ( callable $callback ) : mixed Pass the collection to the given callback and return the result.
pluck ( string $value, string | null $key = null ) : static Get the values of a given key.
pop ( ) : mixed Get and remove the last item from the collection.
prepend ( mixed $value, mixed $key = null ) Push an item onto the beginning of the collection.
pull ( mixed $key, mixed $default = null ) : mixed Get and remove an item from the collection.
push ( mixed $value ) Push an item onto the end of the collection.
put ( mixed $key, mixed $value ) Put an item in the collection by key.
random ( integer $amount = 1 ) : mixed Get one or more items randomly from the collection.
reduce ( callable $callback, mixed $initial = null ) : mixed Reduce the collection to a single value.
reject ( callable | mixed $callback ) : static Create a collection of all elements that do not pass a given truth test.
reverse ( ) : static Reverse items order.
search ( mixed $value, boolean $strict = false ) : mixed Search the collection for a given value and return the corresponding key if successful.
shift ( ) : mixed Get and remove the first item from the collection.
shuffle ( integer $seed = null ) : static Shuffle the items in the collection.
slice ( integer $offset, integer $length = null ) : static Slice the underlying collection array.
sort ( callable $callback = null ) : static Sort through each item with a callback.
sortBy ( callable | string $callback, integer $options = SORT_REGULAR, boolean $descending = false ) : static Sort the collection using the given callback.
sortByDesc ( callable | string $callback, integer $options = SORT_REGULAR ) : static Sort the collection in descending order using the given callback.
splice ( integer $offset, integer | null $length = null, mixed $replacement = [] ) : static Splice a portion of the underlying collection array.
split ( integer $numberOfGroups ) : static Split a collection into a certain number of groups.
sum ( callable | string | null $callback = null ) : mixed Get the sum of the given values.
take ( integer $limit ) : static Take the first or last {$limit} items.
toArray ( ) : array Get the collection of items as a plain array.
toBase ( ) : Collection Get a base Support collection instance from this collection.
toJson ( integer $options ) : string Get the collection of items as JSON.
transform ( callable $callback ) Transform each item in the collection using a callback.
union ( mixed $items ) : static Union the collection with the given items.
unique ( string | callable | null $key = null, boolean $strict = false ) : static Return only unique items from the collection array.
uniqueStrict ( string | callable | null $key = null ) : static Return only unique items from the collection array using strict comparison.
values ( ) : static Reset the keys on the underlying array.
where ( string $key, mixed $operator, mixed $value = null ) : static Filter items by the given key value pair.
whereIn ( string $key, mixed $values, boolean $strict = false ) : static Filter items by the given key value pair.
whereInStrict ( string $key, mixed $values ) : static Filter items by the given key value pair using strict comparison.
whereStrict ( string $key, mixed $value ) : static Filter items by the given key value pair using strict comparison.
zip ( $items ) : static Zip the collection together with one or more arrays.

Protected Methods

Method Description
getArrayableItems ( mixed $items ) : array Results array of items from Collection or Arrayable.
operatorForWhere ( string $key, string $operator, mixed $value ) : Closure Get an operator checker callback.
useAsCallable ( mixed $value ) : boolean Determine if the given value is callable, but not a string.
valueRetriever ( string $value ) : callable Get a value retrieving callback.

Method Details

__construct() public method

Create a new collection.
public __construct ( mixed $items = [] ) : void
$items mixed
return void

__toString() public method

Convert the collection to its string representation.
public __toString ( ) : string
return string

all() public method

Get all of the items in the collection.
public all ( ) : array
return array

average() public method

Alias for the "avg" method.
public average ( callable | string | null $callback = null ) : mixed
$callback callable | string | null
return mixed

avg() public method

Get the average value of a given key.
public avg ( callable | string | null $callback = null ) : mixed
$callback callable | string | null
return mixed

chunk() public method

Chunk the underlying collection array.
public chunk ( integer $size ) : static
$size integer
return static

collapse() public method

Collapse the collection of items into a single array.
public collapse ( ) : static
return static

combine() public method

Create a collection by using this collection for keys and another for its values.
public combine ( mixed $values ) : static
$values mixed
return static

contains() public method

Determine if an item exists in the collection.
public contains ( mixed $key, mixed $value = null ) : boolean
$key mixed
$value mixed
return boolean

containsStrict() public method

Determine if an item exists in the collection using strict comparison.
public containsStrict ( mixed $key, mixed $value = null ) : boolean
$key mixed
$value mixed
return boolean

count() public method

Count the number of items in the collection.
public count ( ) : integer
return integer

diff() public method

Get the items in the collection that are not present in the given items.
public diff ( mixed $items ) : static
$items mixed
return static

diffKeys() public method

Get the items in the collection whose keys are not present in the given items.
public diffKeys ( mixed $items ) : static
$items mixed
return static

each() public method

Execute a callback over each item.
public each ( callable $callback )
$callback callable

every() public method

Create a new collection consisting of every n-th element.
public every ( integer $step, integer $offset ) : static
$step integer
$offset integer
return static

except() public method

Get all items except for those with the specified keys.
public except ( mixed $keys ) : static
$keys mixed
return static

filter() public method

Run a filter over each of the items.
public filter ( callable $callback = null ) : static
$callback callable
return static

first() public method

Get the first item from the collection.
public first ( callable $callback = null, mixed $default = null ) : mixed
$callback callable
$default mixed
return mixed

flatMap() public method

Map a collection and flatten the result by a single level.
public flatMap ( callable $callback ) : static
$callback callable
return static

flatten() public method

Get a flattened array of the items in the collection.
public flatten ( integer $depth = INF ) : static
$depth integer
return static

flip() public method

Flip the items in the collection.
public flip ( ) : static
return static

forPage() public method

"Paginate" the collection by slicing it into a smaller collection.
public forPage ( integer $page, integer $perPage ) : static
$page integer
$perPage integer
return static

forget() public method

Remove an item from the collection by key.
public forget ( string | array $keys )
$keys string | array

get() public method

Get an item from the collection by key.
public get ( mixed $key, mixed $default = null ) : mixed
$key mixed
$default mixed
return mixed

getArrayableItems() protected method

Results array of items from Collection or Arrayable.
protected getArrayableItems ( mixed $items ) : array
$items mixed
return array

getCachingIterator() public method

Get a CachingIterator instance.
public getCachingIterator ( integer $flags = CachingIterator::CALL_TOSTRING ) : CachingIterator
$flags integer
return CachingIterator

getIterator() public method

Get an iterator for the items.

groupBy() public method

Group an associative array by a field or using a callback.
public groupBy ( callable | string $groupBy, boolean $preserveKeys = false ) : static
$groupBy callable | string
$preserveKeys boolean
return static

has() public method

Determine if an item exists in the collection by key.
public has ( mixed $key ) : boolean
$key mixed
return boolean

implode() public method

Concatenate values of a given key as a string.
public implode ( string $value, string $glue = null ) : string
$value string
$glue string
return string

intersect() public method

Intersect the collection with the given items.
public intersect ( mixed $items ) : static
$items mixed
return static

isEmpty() public method

Determine if the collection is empty or not.
public isEmpty ( ) : boolean
return boolean

jsonSerialize() public method

Convert the object into something JSON serializable.
public jsonSerialize ( ) : array
return array

keyBy() public method

Key an associative array by a field or using a callback.
public keyBy ( callable | string $keyBy ) : static
$keyBy callable | string
return static

keys() public method

Get the keys of the collection items.
public keys ( ) : static
return static

last() public method

Get the last item from the collection.
public last ( callable $callback = null, mixed $default = null ) : mixed
$callback callable
$default mixed
return mixed

make() public static method

Create a new collection instance if the value isn't one already.
public static make ( mixed $items = [] ) : static
$items mixed
return static

map() public method

Run a map over each of the items.
public map ( callable $callback ) : static
$callback callable
return static

mapWithKeys() public method

The callback should return an associative array with a single key/value pair.
public mapWithKeys ( callable $callback ) : static
$callback callable
return static

max() public method

Get the max value of a given key.
public max ( callable | string | null $callback = null ) : mixed
$callback callable | string | null
return mixed

median() public method

Get the median of a given key.
public median ( null $key = null ) : mixed | null
$key null
return mixed | null

merge() public method

Merge the collection with the given items.
public merge ( mixed $items ) : static
$items mixed
return static

min() public method

Get the min value of a given key.
public min ( callable | string | null $callback = null ) : mixed
$callback callable | string | null
return mixed

mode() public method

Get the mode of a given key.
public mode ( null $key = null ) : array
$key null
return array

offsetExists() public method

Determine if an item exists at an offset.
public offsetExists ( mixed $key ) : boolean
$key mixed
return boolean

offsetGet() public method

Get an item at a given offset.
public offsetGet ( mixed $key ) : mixed
$key mixed
return mixed

offsetSet() public method

Set the item at a given offset.
public offsetSet ( mixed $key, mixed $value ) : void
$key mixed
$value mixed
return void

offsetUnset() public method

Unset the item at a given offset.
public offsetUnset ( string $key ) : void
$key string
return void

only() public method

Get the items with the specified keys.
public only ( mixed $keys ) : static
$keys mixed
return static

operatorForWhere() protected method

Get an operator checker callback.
protected operatorForWhere ( string $key, string $operator, mixed $value ) : Closure
$key string
$operator string
$value mixed
return Closure

pipe() public method

Pass the collection to the given callback and return the result.
public pipe ( callable $callback ) : mixed
$callback callable
return mixed

pluck() public method

Get the values of a given key.
public pluck ( string $value, string | null $key = null ) : static
$value string
$key string | null
return static

pop() public method

Get and remove the last item from the collection.
public pop ( ) : mixed
return mixed

prepend() public method

Push an item onto the beginning of the collection.
public prepend ( mixed $value, mixed $key = null )
$value mixed
$key mixed

pull() public method

Get and remove an item from the collection.
public pull ( mixed $key, mixed $default = null ) : mixed
$key mixed
$default mixed
return mixed

push() public method

Push an item onto the end of the collection.
public push ( mixed $value )
$value mixed

put() public method

Put an item in the collection by key.
public put ( mixed $key, mixed $value )
$key mixed
$value mixed

random() public method

Get one or more items randomly from the collection.
public random ( integer $amount = 1 ) : mixed
$amount integer
return mixed

reduce() public method

Reduce the collection to a single value.
public reduce ( callable $callback, mixed $initial = null ) : mixed
$callback callable
$initial mixed
return mixed

reject() public method

Create a collection of all elements that do not pass a given truth test.
public reject ( callable | mixed $callback ) : static
$callback callable | mixed
return static

reverse() public method

Reverse items order.
public reverse ( ) : static
return static

shift() public method

Get and remove the first item from the collection.
public shift ( ) : mixed
return mixed

shuffle() public method

Shuffle the items in the collection.
public shuffle ( integer $seed = null ) : static
$seed integer
return static

slice() public method

Slice the underlying collection array.
public slice ( integer $offset, integer $length = null ) : static
$offset integer
$length integer
return static

sort() public method

Sort through each item with a callback.
public sort ( callable $callback = null ) : static
$callback callable
return static

sortBy() public method

Sort the collection using the given callback.
public sortBy ( callable | string $callback, integer $options = SORT_REGULAR, boolean $descending = false ) : static
$callback callable | string
$options integer
$descending boolean
return static

sortByDesc() public method

Sort the collection in descending order using the given callback.
public sortByDesc ( callable | string $callback, integer $options = SORT_REGULAR ) : static
$callback callable | string
$options integer
return static

splice() public method

Splice a portion of the underlying collection array.
public splice ( integer $offset, integer | null $length = null, mixed $replacement = [] ) : static
$offset integer
$length integer | null
$replacement mixed
return static

split() public method

Split a collection into a certain number of groups.
public split ( integer $numberOfGroups ) : static
$numberOfGroups integer
return static

sum() public method

Get the sum of the given values.
public sum ( callable | string | null $callback = null ) : mixed
$callback callable | string | null
return mixed

take() public method

Take the first or last {$limit} items.
public take ( integer $limit ) : static
$limit integer
return static

toArray() public method

Get the collection of items as a plain array.
public toArray ( ) : array
return array

toBase() public method

Get a base Support collection instance from this collection.
public toBase ( ) : Collection
return Collection

toJson() public method

Get the collection of items as JSON.
public toJson ( integer $options ) : string
$options integer
return string

transform() public method

Transform each item in the collection using a callback.
public transform ( callable $callback )
$callback callable

union() public method

Union the collection with the given items.
public union ( mixed $items ) : static
$items mixed
return static

unique() public method

Return only unique items from the collection array.
public unique ( string | callable | null $key = null, boolean $strict = false ) : static
$key string | callable | null
$strict boolean
return static

uniqueStrict() public method

Return only unique items from the collection array using strict comparison.
public uniqueStrict ( string | callable | null $key = null ) : static
$key string | callable | null
return static

useAsCallable() protected method

Determine if the given value is callable, but not a string.
protected useAsCallable ( mixed $value ) : boolean
$value mixed
return boolean

valueRetriever() protected method

Get a value retrieving callback.
protected valueRetriever ( string $value ) : callable
$value string
return callable

values() public method

Reset the keys on the underlying array.
public values ( ) : static
return static

where() public method

Filter items by the given key value pair.
public where ( string $key, mixed $operator, mixed $value = null ) : static
$key string
$operator mixed
$value mixed
return static

whereIn() public method

Filter items by the given key value pair.
public whereIn ( string $key, mixed $values, boolean $strict = false ) : static
$key string
$values mixed
$strict boolean
return static

whereInStrict() public method

Filter items by the given key value pair using strict comparison.
public whereInStrict ( string $key, mixed $values ) : static
$key string
$values mixed
return static

whereStrict() public method

Filter items by the given key value pair using strict comparison.
public whereStrict ( string $key, mixed $value ) : static
$key string
$value mixed
return static

zip() public method

e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); => [[1, 4], [2, 5], [3, 6]]
public zip ( $items ) : static
$items
return static

Property Details

$items protected_oe property

The items contained in the collection.
protected array $items
return array