<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>

CC BY-SA 3.0
2016-06-06T12:45:13.207Z
2016-06-06T17:54:05.307Z
2016-06-06T17:54:05.307Z
Luke Pearce
<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>

CC BY-SA 3.0
2016-06-06T12:23:01.160Z
Is the `primaryImage` asset field definitely in the products section field layout?
Luke Pearce
CC BY-SA 3.0
2016-06-06T17:51:03.973Z
Yes it's an assets field that belongs to the productImages block type in the gallery Matrix.
triplethreat23
<matrix><eager-loading>
Eager Load Matrix Field with Assets Field Type
# 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 %}
                            
                       {% 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 %}`


---

