<p>there is no <code>.with()</code> method for the ElementCriteriaModel nor is it supported by <code>craft.entries</code> as a chainable method, meaning that it can't be chained to <code>craft.entries</code> query like that:</p>

<pre><code>{% for entry in craft.entries.section('news').with['image'] %}
</code></pre>

<p>Especially as an array (hint: you were probably intending to use <code>.with(['image'])</code> instead of <code>.with['image']</code>.</p>

<p>The correct syntax for eager loading elements is:</p>

<pre><code>{% set entries = craft.entries({
    section: 'news',
    with: ['image']
}) %}
</code></pre>

<p>or in your case it can be:</p>

<pre><code>{% for entry in craft.entries({ section: 'news', with:['image'] }) %}
  {% set image = entry.image[0] ?? null %}
  {% if image %}
    &lt;img src="{{ image.url }}"&gt;
  {% endif %}
  {{ entry.title }}
{% endfor %}
</code></pre>

<p>I remember also being frustrated by eager loading in craft (for some reason I also had problems using it at first), but then it turned to be quite useful. It's especially powerful if you'd like to show more then one image per entry, e.g.:</p>

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

<p>Take care</p>

CC BY-SA 3.0
2017-03-11T01:10:33.300Z
This answer is mostly correct, but I confirmed with @bradbell that `.with` should indeed work as a chained method.  

The syntax is `.with(['image']` or if wanting to eager load multiple things, then `.with(['image1','image2'])`
Jeremy Daalder
CC BY-SA 3.0
2017-03-24T10:21:23.363Z
@JeremyDaalder oh nice, couldn't spot that in the official docs though…
Łukasz Mazurek
CC BY-SA 3.0
2017-03-24T22:41:20.433Z
That's why I checked with Brad ;) - might be worth editing that bit off the top above.
Jeremy Daalder
CC BY-SA 3.0
2017-03-09T17:20:15.300Z
2017-03-09T17:20:15.300Z
Łukasz Mazurek
<p>The original answer is good, but as noted, somewhat incorrect, at least concerning a chainable <code>.with()</code> method.</p>

<p>Here's my attempt to explain things: <a href="https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading" rel="nofollow noreferrer">Speed up your Craft CMS Templates with Eager Loading</a></p>

CC BY-SA 3.0
2017-10-02T04:35:35.473Z
2017-10-02T04:35:35.473Z
andrew.welch
<p>I've tried to apply eager loading per Craft's docs, but it doesn't seem to work. When I do this, will I have access to all the entry fields? If not, do I have to include all the fields I want in the with attribute?</p>

<pre><code>{% for entry in craft.entries.section('news').with['image'] %}
  {% set image = entry.image[0] ?? null %}
  {% if image %}
    &lt;img src="{{ image.url }}"&gt;
  {% endif %}
  {{ entry.title }}
{% endfor %}
</code></pre>

<p>When I tried it, the code barfed when trying to output {{ entry.title }}.</p>

CC BY-SA 3.0
2017-03-05T15:46:58.183Z
Would probably be helpful to replace the word `barfed` with the actual error message.
Ben Parizek
<entries><assets><eager-loading>
Trouble understanding eager loading
# Trouble understanding eager loading

(Asked by: username_0 on 2017-03-04)

I've tried to apply eager loading per Craft's docs, but it doesn't seem to work. When I do this, will I have access to all the entry fields? If not, do I have to include all the fields I want in the with attribute?
{% for entry in craft.entries.section('news').with['image'] %}
  {% set image = entry.image[0] ?? null %}
  {% if image %}
    
  {% endif %}
  {{ entry.title }}
{% endfor %}

When I tried it, the code barfed when trying to output {{ entry.title }}.

**Tags:** entries, assets, eager-loading

> **Comments:**
> **username_1**: Would probably be helpful to replace the word `barfed` with the actual error message.  

---

## 2 answers

### Answer by username_2 on 2017-03-09. Score: 1 | Accepted answer

there is no .with() method for the ElementCriteriaModel nor is it supported by craft.entries as a chainable method, meaning that it can't be chained to craft.entries query like that:
{% for entry in craft.entries.section('news').with['image'] %}

Especially as an array (hint: you were probably intending to use .with(['image']) instead of .with['image'].
The correct syntax for eager loading elements is:
{% set entries = craft.entries({
    section: 'news',
    with: ['image']
}) %}

or in your case it can be:
{% for entry in craft.entries({ section: 'news', with:['image'] }) %}
  {% set image = entry.image[0] ?? null %}
  {% if image %}
    
  {% endif %}
  {{ entry.title }}
{% endfor %}

I remember also being frustrated by eager loading in craft (for some reason I also had problems using it at first), but then it turned to be quite useful. It's especially powerful if you'd like to show more then one image per entry, e.g.:
{% for i in entry.image %}
    
{% endfor %}

Take care

> **Comments:**
> **username_3**: This answer is mostly correct, but I confirmed with @bradbell that `.with` should indeed work as a chained method.    The syntax is `.with(['image']` or if wanting to eager load multiple things, then `.with(['image1','image2'])`  
> **username_2**: @JeremyDaalder oh nice, couldn't spot that in the official docs though…  
> **username_3**: That's why I checked with Brad ;) - might be worth editing that bit off the top above.  

---

### Answer by username_4 on 2017-10-02. Score: 2

The original answer is good, but as noted, somewhat incorrect, at least concerning a chainable `.with()` method.

Here's my attempt to explain things: [Speed up your Craft CMS Templates with Eager Loading](https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading)


---

