<p>Eager loading an Assets field simply means that instead of <code>block.sectionBlockImages</code> returning an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a>, it'll return an array. This means that you can't do stuff like <code>block.sectionBlockImages.first()</code>, but have to use brackets notation – i.e. <code>block.sectionBlockImages[0]</code> to access the first Asset.</p>

<p>Looping over the field would look exactly the same, though. So you'll need to change this</p>

<pre><code>{% for images in block.sectionBlockImages %}
    {% set image = block.sectionBlockImages[0] ?? null %}
    &lt;img src="{{ image.url }}"&gt;
{% endfor %}
</code></pre>

<p>to this</p>

<pre><code>{% for image in block.sectionBlockImages %}
    &lt;img src="{{ image.url }}"&gt;
{% endfor %}
</code></pre>

<p>...and you should be good.</p>

CC BY-SA 4.0
2021-01-18T19:21:59.170Z
I get a deprecation error when doing this method now... Craft 3.5.x - it works, but I would like to do it without the deprecation error....
M Smith
CC BY-SA 3.0
2018-01-19T07:36:08.587Z
2018-01-19T07:36:08.587Z
Mats Mikkel Rummelhoff
<p>I'm trying to loop through images that are eager-loaded inside matrix block, but I can't get the images inside a matrix field handle to loop and show each individual image. </p>

<p>The loop I have loops the correct amount of times (based on how many images there are in each matrix block), but it just shows the first image of the loop.</p>

<p>Here's the set up:</p>

<p>Section handle: products
Matrix field handle: imageTextSections
Block type handle: sectionBlock
Field handle: sectionBlockImages
Field type: Assets</p>

<p>I'm trying to loop through images that are eager-loaded inside matrix block, but I can't get the images inside a matrix field to loop and show each individual image. </p>

<p>The loop I have loops the correct amount of times (based on how many images there are in each matrix block), but it just shows the first image of the loop.</p>

<p>Here's the set up:</p>

<p>Section handle: products</p>

<p>Matrix field handle: imageTextSections</p>

<p>Block type handle: sectionBlock</p>

<p>Field handle: sectionBlockImages</p>

<p>Field type: Assets</p>

<pre><code>{% set blocks = entry.imageTextSections.find({
    with: [
        'sectionBlock:sectionBlockImages'
    ]
}) %}

{% for block in blocks %}
...

{% for images in block.sectionBlockImages %}
    {% set image = block.sectionBlockImages[0] ?? null %}
    &lt;img src="{{ image.url }}"&gt;
{% endfor %}

{% endfor %}
</code></pre>

<p>I figured that the following would work, but I get an error stating that the AssetFileModel.0 is not defined.</p>

<pre><code>{% for images in block.sectionBlockImages %}
    {% set image = images[0] ?? null %}
    &lt;img src="{{ image.url }}"&gt;
{% endfor %}
</code></pre>

<p>Any guidance would be greatly appreciated!</p>

CC BY-SA 3.0
2018-01-19T07:38:33.067Z
What happens when you don't overwrite  the asset after you set it correctly? `{% for images in block.sectionBlockImages %}` should be enough, there is no need to do `{% set image = images[0] ?? null %}` since your `image` variable  should be the correct asset
Robin Schambach
<matrix><eager-loading>
Looping through an assets field type within a matrix field that's eager loaded
# Looping through an assets field type within a matrix field that's eager loaded

(Asked by: username_0 on 2018-01-18)

I'm trying to loop through images that are eager-loaded inside matrix block, but I can't get the images inside a matrix field handle to loop and show each individual image. 
The loop I have loops the correct amount of times (based on how many images there are in each matrix block), but it just shows the first image of the loop.
Here's the set up:
Section handle: products
Matrix field handle: imageTextSections
Block type handle: sectionBlock
Field handle: sectionBlockImages
Field type: Assets
I'm trying to loop through images that are eager-loaded inside matrix block, but I can't get the images inside a matrix field to loop and show each individual image. 
The loop I have loops the correct amount of times (based on how many images there are in each matrix block), but it just shows the first image of the loop.
Here's the set up:
Section handle: products
Matrix field handle: imageTextSections
Block type handle: sectionBlock
Field handle: sectionBlockImages
Field type: Assets
{% set blocks = entry.imageTextSections.find({
    with: [
        'sectionBlock:sectionBlockImages'
    ]
}) %}

{% for block in blocks %}
...

{% for images in block.sectionBlockImages %}
    {% set image = block.sectionBlockImages[0] ?? null %}
    
{% endfor %}

{% endfor %}

I figured that the following would work, but I get an error stating that the AssetFileModel.0 is not defined.
{% for images in block.sectionBlockImages %}
    {% set image = images[0] ?? null %}
    
{% endfor %}

Any guidance would be greatly appreciated!

**Tags:** matrix, eager-loading
> **Comments:**
> **username_1**: What happens when you don't overwrite  the asset after you set it correctly? `{% for images in block.sectionBlockImages %}` should be enough, there is no need to do `{% set image = images[0] ?? null %}` since your `image` variable  should be the correct asset  

---

## 1 answers

### Answer by username_2 on 2018-01-19. Score: 2

Eager loading an Assets field simply means that instead of block.sectionBlockImages returning an ElementCriteriaModel, it'll return an array. This means that you can't do stuff like block.sectionBlockImages.first(), but have to use brackets notation – i.e. block.sectionBlockImages[0] to access the first Asset.
Looping over the field would look exactly the same, though. So you'll need to change this
{% for images in block.sectionBlockImages %}
    {% set image = block.sectionBlockImages[0] ?? null %}
    
{% endfor %}

to this
{% for image in block.sectionBlockImages %}
    
{% endfor %}

...and you should be good.

> **Comments:**
> **username_3**: I get a deprecation error when doing this method now... Craft 3.5.x - it works, but I would like to do it without the deprecation error....  

---

