ElementCollection ​
- Type
- Class
- Namespace
- craft\elements
- Inherits
- craft\elements\ElementCollection »
Illuminate\Support\Collection - Since
- 4.3.0
ElementCollection represents a collection of elements.
Public Methods ​
| Method | Description |
|---|---|
| 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() ​
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.
Arguments ​
Returns ​
countBy() ​
Arguments ​
$countBy
Returns ​
\Illuminate\Support\Collection
diff() ​
Returns a new collection with the elements that are not present in the given array.
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.
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.
Arguments ​
$key(integer,\craft\elements\TElement,\Illuminate\Contracts\Support\Arrayable<array-key,int>,iterable<array-key,int>)$default(\craft\elements\TFindDefault)
flatten() ​
Arguments ​
Returns ​
\Illuminate\Support\Collection
flip() ​
Throws ​
fresh() ​
- Since
- 4.10.0
Reloads fresh element instances from the database for all the elements.
Returns ​
static
ids() ​
Returns a collection of the elements’ IDs.
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.
Arguments ​
$items(array<array-key,\craft\elements\TElement>)
Returns ​
static
keys() ​
Returns ​
\Illuminate\Support\Collection
map() ​
Runs a map over each of the items.
Arguments ​
$callback(callable)
mapWithKeys() ​
Runs an associative map over each of the items.
The callback should return an associative array with a single key/value pair.
Arguments ​
$callback
merge() ​
Merge the collection with the given elements.
Any elements with a matching ID and site ID will be replaced.
Arguments ​
$items(iterable<array-key,\craft\elements\TElement>)
Returns ​
static
one() ​
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.
Arguments ​
$keys(\Illuminate\Support\Enumerable<array-key,\craft\elements\TKey>,array<array-key,\craft\elements\TKey>, string, integer, null)
Returns ​
static
pad() ​
Arguments ​
$size$value
Returns ​
\Illuminate\Support\Collection
pluck() ​
Arguments ​
$value$key
Returns ​
\Illuminate\Support\Collection
unique() ​
Return only unique items from the collection.
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.
Arguments ​
Returns ​
Example ​
{# Fetch entries and eager-load the "Related" field’s relations onto them #}
{% set entries = craft.entries()
.collect()
.with(['related']) %}// Fetch entries and eager-load the "Related" field’s relations onto them
$entries = Entry::find()
->collect()
->with(['related']);zip() ​
Arguments ​
$items
Returns ​
\Illuminate\Support\Collection