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
ids()Returns a collection of the elements’ IDs.
one()
with()Eager-loads related elements for the collected elements.

ids()

Returns a collection of the elements’ IDs.

View source

Returns

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

one()

View source

Arguments

  • $callback
  • $default

Returns

\craft\elements\TValue

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

::: code

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']);

:::