Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-18T19:21:59.170Z
Answers.Text: 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....
Answers.UserDisplayName: M Smith
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-19T07:36:08.587Z
Answers.LastActivityDate: 2018-01-19T07:36:08.587Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-19T07:38:33.067Z
Comments.Text: 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
Comments.UserDisplayName: Robin Schambach
Tags: <matrix><eager-loading>
Title: Looping through an assets field type within a matrix field that's eager loaded
ThreadText: # 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 %}
      <img src="{{ image.url }}">
  {% 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 %}
      <img src="{{ image.url }}">
  {% 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](https://craftcms.com/docs/templating/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 %}
      <img src="{{ image.url }}">
  {% endfor %}
  
  ```
  
  to this
  
  ```
  {% for image in block.sectionBlockImages %}
      <img src="{{ image.url }}">
  {% 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....  
  
  ---
  
  