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 %}
<img src="{{ image.url }}">
{% 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 %}
<img src="{{ i.url }}" alt="">
{% endfor %}
Take care
barfed
with the actual error message.