Skip to content

ElementCollection ​

Type
Class
Namespace
craft\elements
Inherits
craft\elements\ElementCollection » Illuminate\Support\Collection
Since
4.3.0

ElementCollection represents a collection of elements.

View source

Public Methods ​

MethodDescription
collapse()
contains()Returns whether an element exists within the collection.
countBy()
diff()Returns a new collection with the elements that are not present in the given array.
except()Returns a new collection with all elements except those with the specified keys.
find()Finds an element in the collection.
flatten()
flip()
fresh()Reloads fresh element instances from the database for all the elements.
ids()Returns a collection of the elements’ IDs.
intersect()Returns a new collection with all the elements present in this collection and the provided array.
keys()
map()Runs a map over each of the items.
mapWithKeys()Runs an associative map over each of the items.
merge()Merge the collection with the given elements.
one()
only()Returns a new collection with only the elements with the specified keys.
pad()
pluck()
unique()Return only unique items from the collection.
with()Eager-loads related elements for the collected elements.
zip()

collapse() ​

View source

Returns ​

\Illuminate\Support\Collection

contains() ​

Returns whether an element exists within the collection.

If $key is an element instance, true will be returned if the collection contains an element with the same ID and site ID.

If $key is an integer, true will be returned in the collection contains an element with that ID.

View source

Arguments ​

Returns ​

boolean

countBy() ​

View source

Arguments ​

  • $countBy

Returns ​

\Illuminate\Support\Collection

diff() ​

Returns a new collection with the elements that are not present in the given array.

View source

Arguments ​

  • $items

except() ​

Returns a new collection with all elements except those with the specified keys.

If $keys is an integer or array of integers, a collection of elements without the same IDs will be returned.

View source

Arguments ​

  • $keys (\Illuminate\Support\Enumerable<array-key,\craft\elements\TKey>, array<array-key,\craft\elements\TKey>, string, integer, null)

Returns ​

static

find() ​

Since
4.10.0

Finds an element in the collection.

If $key is an element instance, the first element with the same ID and site ID.

If $key is an integer, the first element with the same ID will be returned.

View source

Arguments ​

  • $key (integer, \craft\elements\TElement, \Illuminate\Contracts\Support\Arrayable<array-key,int>, iterable<array-key,int>)
  • $default (\craft\elements\TFindDefault)

flatten() ​

View source

Arguments ​

Returns ​

\Illuminate\Support\Collection

flip() ​

View source

Throws ​

fresh() ​

Since
4.10.0

Reloads fresh element instances from the database for all the elements.

View source

Returns ​

static

ids() ​

Returns a collection of the elements’ IDs.

View source

Returns ​

\Illuminate\Support\Collection<\craft\elements\TKey,int>

intersect() ​

Returns a new collection with all the elements present in this collection and the provided array.

View source

Arguments ​

  • $items (array<array-key,\craft\elements\TElement>)

Returns ​

static

keys() ​

View source

Returns ​

\Illuminate\Support\Collection

map() ​

Runs a map over each of the items.

View source

Arguments ​

mapWithKeys() ​

Runs an associative map over each of the items.

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

View source

Arguments ​

  • $callback

merge() ​

Merge the collection with the given elements.

Any elements with a matching ID and site ID will be replaced.

View source

Arguments ​

  • $items (iterable<array-key,\craft\elements\TElement>)

Returns ​

static

one() ​

View source

Arguments ​

  • $callback
  • $default

Returns ​

\craft\elements\TElement –

only() ​

Returns a new collection with only the elements with the specified keys.

If $keys is an integer or array of integers, a collection of elements with the same IDs will be returned.

View source

Arguments ​

  • $keys (\Illuminate\Support\Enumerable<array-key,\craft\elements\TKey>, array<array-key,\craft\elements\TKey>, string, integer, null)

Returns ​

static

pad() ​

View source

Arguments ​

  • $size
  • $value

Returns ​

\Illuminate\Support\Collection

pluck() ​

View source

Arguments ​

  • $value
  • $key

Returns ​

\Illuminate\Support\Collection

unique() ​

Return only unique items from the collection.

View source

Arguments ​

Returns ​

static

with() ​

Eager-loads related elements for the collected elements.

See Eager-Loading Elements for a full explanation of how to work with this parameter.

View source

Arguments ​

Returns ​

$this


Example ​

twig
{# Fetch entries and eager-load the "Related" field’s relations onto them #}
{% set entries = craft.entries()
  .collect()
  .with(['related']) %}
php
// Fetch entries and eager-load the "Related" field’s relations onto them
$entries = Entry::find()
    ->collect()
    ->with(['related']);

zip() ​

View source

Arguments ​

  • $items

Returns ​

\Illuminate\Support\Collection