Answers.Body: <p>The line <code>{% set img = entry.primaryImage[0] ?? null %}</code> is looking for the <code>primaryImage</code> field on the <code>entry</code> inside your <code>for</code> loop. It is not looking for <code>primaryImage</code> for the current matrix field.</p>
  
  <p>If <code>primaryImage</code> is a field within your matrix blog you will need to change the line to </p>
  
  <p><code>{% set img = block.primaryImage[0] ?? null %}</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-06T12:45:13.207Z
Answers.LastActivityDate: 2016-06-06T17:54:05.307Z
Answers.LastEditDate: 2016-06-06T17:54:05.307Z
Answers.UserDisplayName: Luke Pearce
Body: <p>I am trying to eager load an image asset assigned to a matrix. The template is pulling entries in from a section and each entry has the matrix field assigned to it that serves as an image gallery.</p>
  
  <p>The error I am getting back using the syntax below is
  <strong>Craft\EntryModel and its behaviors do not have a method or closure named "primaryImage".</strong></p>
  
  <p>Section Handle: products</p>
  
  <p>Matrix Field Handle: gallery</p>
  
  <p>Block Type Handle: productImages</p>
  
  <p>Field Handle: primaryImage, Field Type: Assets</p>
  
  <pre><code> {% set products = craft.entries({
       section: 'products',
           with:[
                      ['gallery.productImages:primaryImage', {
                        withTransforms: [
                        'heroImage'
                      ]
                }],
            ] 
    }) %}
  </code></pre>
  
  <p>Template Code:</p>
  
  <pre><code> {% for entry in products %}
        {% for block in entry.gallery %}
              {% if block.type == 'productImages' %}
                   {% set img = entry.primaryImage[0] ?? null %}
                         {% if img %}
                              &lt;img src="{{ img.url('heroImage') }}"&gt;
                         {% endif %}
              {% endif %}
        {% endfor %}
   {% endfor %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-06T12:23:01.160Z
Comments.Text: Is the `primaryImage` asset field definitely in the products section field layout?
Comments.UserDisplayName: Luke Pearce
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-06T17:51:03.973Z
Comments.Text: Yes it's an assets field that belongs to the productImages block type in the gallery Matrix.
Comments.UserDisplayName: triplethreat23
Tags: <matrix><eager-loading>
Title: Eager Load Matrix Field with Assets Field Type
ThreadText: # Eager Load Matrix Field with Assets Field Type
  
  (Asked by: username_0 on 2016-06-03)
  
  I am trying to eager load an image asset assigned to a matrix. The template is pulling entries in from a section and each entry has the matrix field assigned to it that serves as an image gallery.
  
  The error I am getting back using the syntax below is
  **Craft\EntryModel and its behaviors do not have a method or closure named "primaryImage".**
  
  Section Handle: products
  
  Matrix Field Handle: gallery
  
  Block Type Handle: productImages
  
  Field Handle: primaryImage, Field Type: Assets
  
  ```
   {% set products = craft.entries({
       section: 'products',
           with:[
                      ['gallery.productImages:primaryImage', {
                        withTransforms: [
                        'heroImage'
                      ]
                }],
            ] 
    }) %}
  
  ```
  
  Template Code:
  
  ```
   {% for entry in products %}
        {% for block in entry.gallery %}
              {% if block.type == 'productImages' %}
                   {% set img = entry.primaryImage[0] ?? null %}
                         {% if img %}
                              <img src="{{ img.url('heroImage') }}">
                         {% endif %}
              {% endif %}
        {% endfor %}
   {% endfor %}
  
  ```
  
  **Tags:** matrix, eager-loading
  
  > **Comments:**
  > **username_1**: Is the `primaryImage` asset field definitely in the products section field layout?  
  > **username_0**: Yes it's an assets field that belongs to the productImages block type in the gallery Matrix.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-06-06. Score: 4 | Accepted answer
  
  The line `{% set img = entry.primaryImage[0] ?? null %}` is looking for the `primaryImage` field on the `entry` inside your `for` loop. It is not looking for `primaryImage` for the current matrix field.
  
  If `primaryImage` is a field within your matrix blog you will need to change the line to
  
  `{% set img = block.primaryImage[0] ?? null %}`
  
  
  ---
  
  