<p>A good thing to know about Craft is that all relational fields (such as your Assets field <code>galleryEntry.assetGallery</code>, for instance) return <a href="https://craftcms.com/docs/3.x/element-queries.html" rel="nofollow noreferrer">element query</a> objects. So, all methods and parameters available to element queries are available on that object – including the <code>with()</code> param which can be used to eager-load relations contained in said field.</p>
<p>To eager-load the <code>workCategory</code> categories for the related assets in the <code>assetGallery</code> field, all you'll need to do is add that <code>with()</code> parameter to your existing <code>assetGallery</code> query:</p>
<pre><code>{% set relatedAssets = galleryEntry.assetGallery.with(['workCategory']).all() %}
</code></pre>
<p>To take it one step further, you can even leverage <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html#eager-loading-nested-sets-of-elements" rel="nofollow noreferrer">nested eager-loading</a> when you first pull the <code>galleryEntry</code> entry, which would reduce the number of queries even further. The below example would eager-load both the assets in the gallery entry's <code>galleryEntry</code> field <em>and</em> the categories in those assets' <code>workCategory</code> field (note that the below example assumes you have an exhibition entry set to a <code>entry</code> variable, and that the gallery entry is related to that entry via a <code>galleryEntry</code> Entries field):</p>
<pre><code>{% set galleryEntry = entry.galleryEntry.with(['assetGallery.workCategory']).one() %}
</code></pre>
<p>(Note that in terms of performance the above is probably not noticeable if there's only ever a single <code>galleryEntry</code> – but it can be super effective if you're querying multiple elements, with multiple nested related elements.)</p>
<p>Beyond the above, I recommend looking over <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html" rel="nofollow noreferrer">the official docs on eager-loading</a> - they are pretty detailed and cover most common use cases, including nesting and Matrix fields.</p>

CC BY-SA 4.0
2022-03-04T00:08:07.453Z
2022-03-04T00:17:31.930Z
2022-03-04T00:17:31.930Z
Mats Mikkel Rummelhoff
<p>I am looking for some advice on how to best streamline a gallery system on a site, but having trouble wrapping my head around the syntax needed to eager load nested asset fields with categories inside a related entry.  Any help is appreciated!</p>
<p>Structure:</p>
<ul>
<li>The &quot;exhibition&quot; section has an &quot;Entries&quot; field named galleryEntry</li>
<li>That field relates to entries in the &quot;gallery&quot; section.</li>
<li>The &quot;gallery&quot; section has an &quot;Assets&quot; field called assetGallery that can contain multiple images</li>
<li>These images have a title and a &quot;category&quot; field called workCategory.</li>
</ul>
<p>In the /exhibition/_entry template, I'm trying to display a gallery of images with asset thumbnail, asset title, and asset category.  Simplified code:</p>
<pre><code>{% if galleryEntry|length %}

    {% set relatedAssets = galleryEntry.assetGallery.all() %}
    
    &lt;div class=&quot;gallery&quot;&gt;
    
    {% for work in relatedAssets %}
        
        &lt;figure&gt;
        &lt;img class=&quot;thumb&quot; src=&quot;{{ work.url }}&quot; alt=&quot;{{ work.title }}&quot;/&gt;
        &lt;figcaption&gt;
            Title: {{ work.title }} / 
            Category: {{ work.workCategory.one().title}}
        &lt;/figcaption&gt;
        &lt;/figure&gt;
        
    {% endfor %}
    
    &lt;/div&gt;
    
{% endif %}
</code></pre>

<assets><categories><relations><performance><eager-loading>
Eager loading categories and assets inside related entries for gallery
# Eager loading categories and assets inside related entries for gallery

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

I am looking for some advice on how to best streamline a gallery system on a site, but having trouble wrapping my head around the syntax needed to eager load nested asset fields with categories inside a related entry.  Any help is appreciated!
Structure:

The "exhibition" section has an "Entries" field named galleryEntry
That field relates to entries in the "gallery" section.
The "gallery" section has an "Assets" field called assetGallery that can contain multiple images
These images have a title and a "category" field called workCategory.

In the /exhibition/_entry template, I'm trying to display a gallery of images with asset thumbnail, asset title, and asset category.  Simplified code:
{% if galleryEntry|length %}

    {% set relatedAssets = galleryEntry.assetGallery.all() %}
    
    
    
    {% for work in relatedAssets %}
        
        
        
        
            Title: {{ work.title }} / 
            Category: {{ work.workCategory.one().title}}
        
        
        
    {% endfor %}
    
    
    
{% endif %}

**Tags:** assets, categories, relations, performance, eager-loading

---

## 1 answers

### Answer by username_1 on 2022-03-04. Score: 3

A good thing to know about Craft is that all relational fields (such as your Assets field galleryEntry.assetGallery, for instance) return element query objects. So, all methods and parameters available to element queries are available on that object – including the with() param which can be used to eager-load relations contained in said field.
To eager-load the workCategory categories for the related assets in the assetGallery field, all you'll need to do is add that with() parameter to your existing assetGallery query:
{% set relatedAssets = galleryEntry.assetGallery.with(['workCategory']).all() %}

To take it one step further, you can even leverage nested eager-loading when you first pull the galleryEntry entry, which would reduce the number of queries even further. The below example would eager-load both the assets in the gallery entry's galleryEntry field and the categories in those assets' workCategory field (note that the below example assumes you have an exhibition entry set to a entry variable, and that the gallery entry is related to that entry via a galleryEntry Entries field):
{% set galleryEntry = entry.galleryEntry.with(['assetGallery.workCategory']).one() %}

(Note that in terms of performance the above is probably not noticeable if there's only ever a single galleryEntry – but it can be super effective if you're querying multiple elements, with multiple nested related elements.)
Beyond the above, I recommend looking over the official docs on eager-loading - they are pretty detailed and cover most common use cases, including nesting and Matrix fields.


---

