<p>Eager-loading turns relational fields into arrays of elements (i.e. the same thing that happens if you do <code>entry.teamCategory.all()</code>), so your use of the bracket syntax is technically correct.</p>
<p>The problem is that you're doing</p>
<pre><code>{% set teamCats = entry.teamCategory[0] ?? null %}
</code></pre>
<p>...which will set the <code>teamCats</code> variable to the <em>first</em> category from the eager-loaded <code>teamCategory</code> field (or <code>null</code>, if there are no categories in the array). Essentially, this is the proper alternative to <code>entry.teamCategory.one()</code> when the field is eager-loaded.</p>
<p>Which is why your <code>{% for %}</code> loops doesn't output anything – you're trying to loop on a single element, not the entire array of categories.</p>
<p>Since the <code>entry.teamCategory</code> and <code>entry.articleCats</code> fields are already arrays, the below would work:</p>
<pre><code>{% set teamCats = entry.teamCategory %}
{% set articleCats = entry.articleCats %}
</code></pre>
<p>...although from looking at the example you don't really need those variables, so technically you could just delete those and do <code>{% for cat in entry.articleCategories %}</code> and <code>{% for logo in entry.teamCategory %}</code>.</p>

CC BY-SA 4.0
2022-03-29T07:33:54.450Z
2022-03-29T07:33:54.450Z
Mats Mikkel Rummelhoff
<p>I have two category fields, <code>teamCategory</code> and <code>articleCategories</code>, on a news listing page that I'm trying to eager load. Problem is, when I add the eager loading it no longer outputs the categories. I should say right off that I'm not that experienced with Craft so I'm probably misunderstanding something.</p>
<p>From <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html#accessing-eager-loaded-elements" rel="nofollow noreferrer">the documentation I've tried the array syntax</a> but I'm either applying it wrong or there is a different approach for categories(?).</p>
<pre><code>{% paginate craft.entries.section('news').with(['teamCategory', 'articleCategories']).limit(25) as pageInfo, pageEntries %}

{% for entry in pageEntries %} 

    {% set teamCats = entry.teamCategory[0] ?? null %}
    {% set articleCats = entry.articleCats[0] ?? null %}

    {% if articleCats|length %}
        {% for cat in articleCats %}
            {{ cat.slug }}
        {% endfor %}
    {% endif %}
    {% if teamCats|length %}
        {% for logo in teamCats %}
            {{ logo.title }}
        {% endfor %}
    {% endif %}
{% endfor %}
</code></pre>
<p>All the other posts that I could find were about more complex applications, I'm just looking for the basic function of eager loading a category field and then how to access it in the template.</p>
<p>I did try the non eager loaded syntax:</p>
<pre><code>{% set teamCats = entry.teamCategory.all() %} 
{% set articleCats = entry.articleCategories.all() %}
</code></pre>
<p>Without eager loading that works, and I actually have another eager loaded field that I didn't include in this example (matrix) that works fine. It's just the categories that I'm struggling with.</p>

<categories><pagination><eager-loading>
How do you Eager Load Category Fields with Pagination
# How do you Eager Load Category Fields with Pagination

(Asked by: username_0 on 2022-03-28)

I have two category fields, teamCategory and articleCategories, on a news listing page that I'm trying to eager load. Problem is, when I add the eager loading it no longer outputs the categories. I should say right off that I'm not that experienced with Craft so I'm probably misunderstanding something.
From the documentation I've tried the array syntax but I'm either applying it wrong or there is a different approach for categories(?).
{% paginate craft.entries.section('news').with(['teamCategory', 'articleCategories']).limit(25) as pageInfo, pageEntries %}

{% for entry in pageEntries %} 

    {% set teamCats = entry.teamCategory[0] ?? null %}
    {% set articleCats = entry.articleCats[0] ?? null %}

    {% if articleCats|length %}
        {% for cat in articleCats %}
            {{ cat.slug }}
        {% endfor %}
    {% endif %}
    {% if teamCats|length %}
        {% for logo in teamCats %}
            {{ logo.title }}
        {% endfor %}
    {% endif %}
{% endfor %}

All the other posts that I could find were about more complex applications, I'm just looking for the basic function of eager loading a category field and then how to access it in the template.
I did try the non eager loaded syntax:
{% set teamCats = entry.teamCategory.all() %} 
{% set articleCats = entry.articleCategories.all() %}

Without eager loading that works, and I actually have another eager loaded field that I didn't include in this example (matrix) that works fine. It's just the categories that I'm struggling with.

**Tags:** categories, pagination, eager-loading


---

## 1 answers

### Answer by username_1 on 2022-03-29. Score: 1 | Accepted answer

Eager-loading turns relational fields into arrays of elements (i.e. the same thing that happens if you do entry.teamCategory.all()), so your use of the bracket syntax is technically correct.
The problem is that you're doing
{% set teamCats = entry.teamCategory[0] ?? null %}

...which will set the teamCats variable to the first category from the eager-loaded teamCategory field (or null, if there are no categories in the array). Essentially, this is the proper alternative to entry.teamCategory.one() when the field is eager-loaded.
Which is why your {% for %} loops doesn't output anything – you're trying to loop on a single element, not the entire array of categories.
Since the entry.teamCategory and entry.articleCats fields are already arrays, the below would work:
{% set teamCats = entry.teamCategory %}
{% set articleCats = entry.articleCats %}

...although from looking at the example you don't really need those variables, so technically you could just delete those and do {% for cat in entry.articleCategories %} and {% for logo in entry.teamCategory %}.


---

