Skip to content

[4.x] Eager loaded elements are null #12247

Answered by brianjhanson
martinherweg asked this question in Q&A
Discussion options

You must be logged in to vote

Edit because I was wrong.

Since Craft 4, eager loaded elements are returned as an craft\elements\ElementCollection which doesn't care if you use [0] or .one().

The (mostly) original answer below still applies to Craft 3 though.

You'll need to use the array syntax when accessing eager loaded elements.

{% set newsQuery = craft.entries.section('news').with(['heroImage', 'topics', 'category']).limit(4).all() %}
{% for news in newsQuery %}
  {{ d(news.topics[0]) }}
{% endfor %}

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@martinherweg
Comment options

@brianjhanson
Comment options

Answer selected by brianjhanson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants