Answers.Body: <p>The reason this doesn't work is because <code>.type('image')</code> won't work with eager loading. Eager loading removes the ability to use <code>ElementCriteriaModel</code> methods.</p>
  <p>From the docs:</p>
  <blockquote>
  <p>When the assets aren’t eager-loaded, entry.assetsField gives you an ElementCriteriaModel that is preconfigured to return the related assets once they’re requested (e.g. when first() is called).</p>
  <p>However when the assets are eager-loaded, entry.assetsField gets overwritten with an array of the eager-loaded assets. So first(), find(), and other ElementCriteriaModel methods are not available. Instead you must stick to the standard array syntaxes.</p>
  </blockquote>
  <p>As a work around, you could use a for loop to match only Matrix blocks of a particular type instead:</p>
  <pre><code>{% for block in entry.projectMatrix if block.type == 'imageBlock' %}
    {% set matrixImage = block.imageFile[0] %}
  {% endfor %}
  </code></pre>
  <p>If you only want the first image block type:</p>
  <pre><code>{% for block in entry.projectMatrix if block.type == 'imageBlock' %}
    {% if loop.index == 1 %}
      {% set matrixImage = block.imageFile[0] %}
    {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-15T11:31:10.453Z
Answers.Text: Ah! It makes sense, thank you for explaining :)
Answers.UserDisplayName: Slebbo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-14T19:34:22.960Z
Answers.LastActivityDate: 2018-03-14T19:34:22.960Z
Answers.LastEditDate: 2020-06-17T08:56:27.903Z
Answers.UserDisplayName: Alex Roper
Body: <p>I've been using <a href="https://craftcms.com/docs/templating/eager-loading-elements#eager-loading-elements-related-to-matrix-blocks" rel="nofollow noreferrer">this example</a> to eager load images, stored in an Assets field which is located in a Matrix field.</p>
  
  <p>My query looks like this:</p>
  
  <pre><code>{% set entries = craft.entries.section('project')
  .with(['projectMatrix.imageBlock:imageFile']) %}
  </code></pre>
  
  <p>Where <code>projectMatrix</code> is a Matrix field, <code>imageBlock</code> is a Matrix block type and <code>imageFile</code> is an Asset field inside <code>imageBlock</code>.</p>
  
  <p>But how do I reference the that image in my templates?</p>
  
  <pre><code>{% for entry in entries %}
  
  {% set matrixImage = entry.projectMatrix.type('imageBlock')[0].imageFile[0] ?? null %}
  
  ...
  
  {% endfor %}
  </code></pre>
  
  <p>This is always be <code>null</code>, when eager loaded</p>
  
Tags: <templating><matrix><eager-loading><assets-field>
Title: Eager-Loading Assets in Matrix
ThreadText: # Eager-Loading Assets in Matrix
  
  (Asked by: username_0 on 2018-03-14)
  
  I've been using [this example](https://craftcms.com/docs/templating/eager-loading-elements#eager-loading-elements-related-to-matrix-blocks) to eager load images, stored in an Assets field which is located in a Matrix field.
  
  My query looks like this:
  
  ```
  {% set entries = craft.entries.section('project')
  .with(['projectMatrix.imageBlock:imageFile']) %}
  
  ```
  
  Where `projectMatrix` is a Matrix field, `imageBlock` is a Matrix block type and `imageFile` is an Asset field inside `imageBlock`.
  
  But how do I reference the that image in my templates?
  
  ```
  {% for entry in entries %}
  
  {% set matrixImage = entry.projectMatrix.type('imageBlock')[0].imageFile[0] ?? null %}
  
  ...
  
  {% endfor %}
  
  ```
  
  This is always be `null`, when eager loaded
  
  **Tags:** templating, matrix, eager-loading, assets-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-14. Score: 4 | Accepted answer
  
  The reason this doesn't work is because `.type('image')` won't work with eager loading. Eager loading removes the ability to use `ElementCriteriaModel` methods.
  
  From the docs:
  
  > When the assets aren’t eager-loaded, entry.assetsField gives you an ElementCriteriaModel that is preconfigured to return the related assets once they’re requested (e.g. when first() is called).
  >
  > However when the assets are eager-loaded, entry.assetsField gets overwritten with an array of the eager-loaded assets. So first(), find(), and other ElementCriteriaModel methods are not available. Instead you must stick to the standard array syntaxes.
  
  As a work around, you could use a for loop to match only Matrix blocks of a particular type instead:
  
  ```
  {% for block in entry.projectMatrix if block.type == 'imageBlock' %}
    {% set matrixImage = block.imageFile[0] %}
  {% endfor %}
  
  ```
  
  If you only want the first image block type:
  
  ```
  {% for block in entry.projectMatrix if block.type == 'imageBlock' %}
    {% if loop.index == 1 %}
      {% set matrixImage = block.imageFile[0] %}
    {% endif %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Ah! It makes sense, thank you for explaining :)  
  
  ---
  
  