class Collection implements ArrayAccess, ArrayableInterface, Countable, IteratorAggregate, JsonableInterface, JsonSerializable (View source)

Methods

void
__construct(array $items = array())

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.

collapse()

Collapse the collection items into a single array.

bool
contains(mixed $value)

Determine if an item exists in the collection.

diff(Collection|ArrayableInterface|array $items)

Diff the collection with the given items.

$this
each(Closure $callback)

Execute a callback over each item.

fetch(string $key)

Fetch a nested element of the collection.

filter(Closure $callback)

Run a filter over each of the items.

mixed|null
first(Closure $callback = null, mixed $default = null)

Get the first item from the collection.

flatten()

Get a flattened array of the items in the collection.

flip()

Flip the items in the collection.

void
forget(mixed $key)

Remove an item from the collection by key.

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

Get an item from the collection by key.

groupBy(callable|string $groupBy)

Group an associative array by a field or Closure value.

keyBy(string $keyBy)

Key an associative array by a field.

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.

intersect(Collection|ArrayableInterface|array $items)

Intersect the collection with the given items.

bool
isEmpty()

Determine if the collection is empty or not.

array
keys()

Get the keys of the collection items.

mixed|null
last()

Get the last item from the collection.

array
lists(string $value, string $key = null)

Get an array with the values of a given key.

map(Closure $callback)

Run a map over each of the items.

merge(Collection|ArrayableInterface|array $items)

Merge the collection with the given items.

mixed|null
pop()

Get and remove the last item from the collection.

void
prepend(mixed $value)

Push an item onto the beginning of the collection.

void
push(mixed $value)

Push an item onto the end of the collection.

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

Pulls an item from the collection.

void
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.

reject(Closure|mixed $callback)

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

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|null
shift()

Get and remove the first item from the collection.

$this
shuffle()

Shuffle the items in the collection.

slice(int $offset, int $length = null, bool $preserveKeys = false)

Slice the underlying collection array.

chunk(int $size, bool $preserveKeys = false)

Chunk the underlying collection array.

$this
sort(Closure $callback)

Sort through each item with a callback.

$this
sortBy(Closure|string $callback, int $options = SORT_REGULAR, bool $descending = false)

Sort the collection using the given Closure.

$this
sortByDesc(Closure|string $callback, int $options = SORT_REGULAR)

Sort the collection in descending order using the given Closure.

splice(int $offset, int $length, mixed $replacement = array())

Splice portion of the underlying collection array.

mixed
sum(Closure $callback = null)

Get the sum of the given values.

take(int $limit = null)

Take the first or last {$limit} items.

$this
transform(Closure $callback)

Transform each item in the collection using a callback.

unique()

Return only unique items from the collection array.

values()

Reset the keys on the underlying array.

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.

getIterator()

Get an iterator for the items.

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.

Details

at line line 28
void __construct(array $items = array())

Create a new collection.

Parameters

array $items

Return Value

void

at line line 39
static Collection make(mixed $items)

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

Parameters

mixed $items

Return Value

Collection

at line line 53
array all()

Get all of the items in the collection.

Return Value

array

at line line 63
Collection collapse()

Collapse the collection items into a single array.

Return Value

Collection

at line line 83
bool contains(mixed $value)

Determine if an item exists in the collection.

Parameters

mixed $value

Return Value

bool

at line line 99
Collection diff(Collection|ArrayableInterface|array $items)

Diff the collection with the given items.

Parameters

Collection|ArrayableInterface|array $items

Return Value

Collection

at line line 110
$this each(Closure $callback)

Execute a callback over each item.

Parameters

Closure $callback

Return Value

$this

at line line 123
Collection fetch(string $key)

Fetch a nested element of the collection.

Parameters

string $key

Return Value

Collection

at line line 134
Collection filter(Closure $callback)

Run a filter over each of the items.

Parameters

Closure $callback

Return Value

Collection

at line line 146
mixed|null first(Closure $callback = null, mixed $default = null)

Get the first item from the collection.

Parameters

Closure $callback
mixed $default

Return Value

mixed|null

at line line 161
Collection flatten()

Get a flattened array of the items in the collection.

Return Value

Collection

at line line 171
Collection flip()

Flip the items in the collection.

Return Value

Collection

at line line 182
void forget(mixed $key)

Remove an item from the collection by key.

Parameters

mixed $key

Return Value

void

at line line 194
mixed get(mixed $key, mixed $default = null)

Get an item from the collection by key.

Parameters

mixed $key
mixed $default

Return Value

mixed

at line line 210
Collection groupBy(callable|string $groupBy)

Group an associative array by a field or Closure value.

Parameters

callable|string $groupBy

Return Value

Collection

at line line 246
Collection keyBy(string $keyBy)

Key an associative array by a field.

Parameters

string $keyBy

Return Value

Collection

at line line 266
bool has(mixed $key)

Determine if an item exists in the collection by key.

Parameters

mixed $key

Return Value

bool

at line line 278
string implode(string $value, string $glue = null)

Concatenate values of a given key as a string.

Parameters

string $value
string $glue

Return Value

string

at line line 289
Collection intersect(Collection|ArrayableInterface|array $items)

Intersect the collection with the given items.

Parameters

Collection|ArrayableInterface|array $items

Return Value

Collection

at line line 299
bool isEmpty()

Determine if the collection is empty or not.

Return Value

bool

at line line 309
array keys()

Get the keys of the collection items.

Return Value

array

at line line 319
mixed|null last()

Get the last item from the collection.

Return Value

mixed|null

at line line 331
array lists(string $value, string $key = null)

Get an array with the values of a given key.

Parameters

string $value
string $key

Return Value

array

at line line 342
Collection map(Closure $callback)

Run a map over each of the items.

Parameters

Closure $callback

Return Value

Collection

at line line 353
Collection merge(Collection|ArrayableInterface|array $items)

Merge the collection with the given items.

Parameters

Collection|ArrayableInterface|array $items

Return Value

Collection

at line line 363
mixed|null pop()

Get and remove the last item from the collection.

Return Value

mixed|null

at line line 374
void prepend(mixed $value)

Push an item onto the beginning of the collection.

Parameters

mixed $value

Return Value

void

at line line 385
void push(mixed $value)

Push an item onto the end of the collection.

Parameters

mixed $value

Return Value

void

at line line 397
mixed pull(mixed $key, mixed $default = null)

Pulls an item from the collection.

Parameters

mixed $key
mixed $default

Return Value

mixed

at line line 409
void put(mixed $key, mixed $value)

Put an item in the collection by key.

Parameters

mixed $key
mixed $value

Return Value

void

at line line 420
mixed random(int $amount = 1)

Get one or more items randomly from the collection.

Parameters

int $amount

Return Value

mixed

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

Reduce the collection to a single value.

Parameters

callable $callback
mixed $initial

Return Value

mixed

at line line 447
Collection reject(Closure|mixed $callback)

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

Parameters

Closure|mixed $callback

Return Value

Collection

at line line 468
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

at line line 490
mixed|null shift()

Get and remove the first item from the collection.

Return Value

mixed|null

at line line 500
$this shuffle()

Shuffle the items in the collection.

Return Value

$this

at line line 515
Collection slice(int $offset, int $length = null, bool $preserveKeys = false)

Slice the underlying collection array.

Parameters

int $offset
int $length
bool $preserveKeys

Return Value

Collection

at line line 527
Collection chunk(int $size, bool $preserveKeys = false)

Chunk the underlying collection array.

Parameters

int $size
bool $preserveKeys

Return Value

Collection

at line line 545
$this sort(Closure $callback)

Sort through each item with a callback.

Parameters

Closure $callback

Return Value

$this

at line line 560
$this sortBy(Closure|string $callback, int $options = SORT_REGULAR, bool $descending = false)

Sort the collection using the given Closure.

Parameters

Closure|string $callback
int $options
bool $descending

Return Value

$this

at line line 598
$this sortByDesc(Closure|string $callback, int $options = SORT_REGULAR)

Sort the collection in descending order using the given Closure.

Parameters

Closure|string $callback
int $options

Return Value

$this

at line line 611
Collection splice(int $offset, int $length, mixed $replacement = array())

Splice portion of the underlying collection array.

Parameters

int $offset
int $length
mixed $replacement

Return Value

Collection

at line line 622
mixed sum(Closure $callback = null)

Get the sum of the given values.

Parameters

Closure $callback

Return Value

mixed

at line line 647
Collection take(int $limit = null)

Take the first or last {$limit} items.

Parameters

int $limit

Return Value

Collection

at line line 660
$this transform(Closure $callback)

Transform each item in the collection using a callback.

Parameters

Closure $callback

Return Value

$this

at line line 672
Collection unique()

Return only unique items from the collection array.

Return Value

Collection

at line line 682
Collection values()

Reset the keys on the underlying array.

Return Value

Collection

at line line 708
array toArray()

Get the collection of items as a plain array.

Return Value

array

at line line 722
array jsonSerialize()

Convert the object into something JSON serializable.

Return Value

array

at line line 733
string toJson(int $options)

Get the collection of items as JSON.

Parameters

int $options

Return Value

string

at line line 743
ArrayIterator getIterator()

Get an iterator for the items.

Return Value

ArrayIterator

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

Get a CachingIterator instance.

Parameters

int $flags

Return Value

CachingIterator

at line line 764
int count()

Count the number of items in the collection.

Return Value

int

at line line 775
bool offsetExists(mixed $key)

Determine if an item exists at an offset.

Parameters

mixed $key

Return Value

bool

at line line 786
mixed offsetGet(mixed $key)

Get an item at a given offset.

Parameters

mixed $key

Return Value

mixed

at line line 798
void offsetSet(mixed $key, mixed $value)

Set the item at a given offset.

Parameters

mixed $key
mixed $value

Return Value

void

at line line 816
void offsetUnset(string $key)

Unset the item at a given offset.

Parameters

string $key

Return Value

void

at line line 826
string __toString()

Convert the collection to its string representation.

Return Value

string