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()
render()Renders the elements using their partial templates.
with()Eager-loads related elements for the collected elements.

ids()

Returns a collection of the elements’ IDs.

View source

Returns

\Illuminate\Support\Collection<array-key,int>

one()

View source

Arguments

  • $callback
  • $default

Returns

\craft\elements\TValue

render()

Since
5.0.0

Renders the elements using their partial templates.

If no partial template exists for an element, its string representation will be output instead.

See also craft\helpers\ElementHelper::renderElements()View source

Arguments

Returns

\Twig\Markup

Throws

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