Understanding Eager Loading I am trying to get to grips with Eager Loading. I have the following code:

{% for article in entry.relatedArticles %}
&lt;div class="c-card // l-card--third"&gt;
    &lt;a href="{{ article.url }}"&gt;
        &lt;img src="{{ article.bannerImage.first().getUrl('cardImage') }}" alt="{{ article.title }}" class="c-card__img"&gt;
        &lt;div class="c-card__bar // js_fitcontent"&gt;
            {% set array = article.title|split(' ', 2) %}
            &lt;p class="c-card__title"&gt;&lt;strong&gt;{{ attribute(array, 0) }}&lt;/strong&gt; {{ attribute(array, 1) }}&lt;/p&gt;
            &lt;p class="c-card__msg // js-fittext"&gt;Posted on {{ helpers.date_formatted(entry.postDate) }}&lt;/p&gt;
        &lt;/div&gt;

        &lt;div class="c-card__over"&gt;
            &lt;div class="c-card__over-content"&gt;
                &lt;div class="animate"&gt;
                    &lt;p class="c-card__title"&gt;&lt;strong&gt;Related&lt;/strong&gt; Article&lt;/p&gt;
                    &lt;p class="c-card__msg  // half-margin-bottom"&gt;
                        &lt;strong&gt;{{ article.title }}&lt;/strong&gt;&lt;br /&gt;
                        &lt;time datetime="{{ entry.postDate|date('Y-m-d H:i:s') }}" class="date"&gt;
                            &lt;svg role="img" class="icon // icon-date" title="Date Icon"&gt;
                                &lt;use xlink:href="/assets/img/icons.svg#icon-date"&gt;&lt;/use&gt;
                            &lt;/svg&gt;{{ helpers.date_formatted(entry.postDate) }}
                        &lt;/time&gt;
                    &lt;/p&gt;
                    &lt;p class="btn // btn--sub"&gt;Read Article&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/a&gt;
&lt;/div&gt;
{% endfor %}


Looking on the console this combined with the other page requests totals 76 requests. I thought I perhaps should add some eager loading 9at least tot he image transform) So I tried the following:

{% for article in entry.relatedArticles.withTransforms('cardImage') %}
&lt;div class="c-card // l-card--third"&gt;
    &lt;a href="{{ article.url }}"&gt;
        &lt;img src="{{ article.bannerImage.first().getUrl('cardImage') }}" alt="{{ article.title }}" class="c-card__img"&gt;
        &lt;div class="c-card__bar // js_fitcontent"&gt;
            {% set array = article.title|split(' ', 2) %}
            &lt;p class="c-card__title"&gt;&lt;strong&gt;{{ attribute(array, 0) }}&lt;/strong&gt; {{ attribute(array, 1) }}&lt;/p&gt;
            &lt;p class="c-card__msg // js-fittext"&gt;Posted on {{ helpers.date_formatted(entry.postDate) }}&lt;/p&gt;
        &lt;/div&gt;

        &lt;div class="c-card__over"&gt;
            &lt;div class="c-card__over-content"&gt;
                &lt;div class="animate"&gt;
                    &lt;p class="c-card__title"&gt;&lt;strong&gt;Related&lt;/strong&gt; Article&lt;/p&gt;
                    &lt;p class="c-card__msg  // half-margin-bottom"&gt;
                        &lt;strong&gt;{{ article.title }}&lt;/strong&gt;&lt;br /&gt;
                        &lt;time datetime="{{ entry.postDate|date('Y-m-d H:i:s') }}" class="date"&gt;
                            &lt;svg role="img" class="icon // icon-date" title="Date Icon"&gt;
                                &lt;use xlink:href="/assets/img/icons.svg#icon-date"&gt;&lt;/use&gt;
                            &lt;/svg&gt;{{ helpers.date_formatted(entry.postDate) }}
                        &lt;/time&gt;
                    &lt;/p&gt;
                    &lt;p class="btn // btn--sub"&gt;Read Article&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/a&gt;
&lt;/div&gt;
{% endfor %}


However, this also still results in 76 requests. 

So my question is this; Have I misunderstood when I need to use Eager Loading? or is my code incorrect and thus eager loading isn't actually taking place?

Thanks.
Your syntax is a bit off.

The withTransforms parameter is an Asset criteria, meaning it is only relevant when you're pulling Assets. Appending it to entry.relatedArticles (an Entries field, returning an ElementCriteriaModel) like you're currently attempting does nothing, but this does:

{% set assets = entry.someAssetsField.find({
    withTransforms: ['someTransform', 'someOtherTransform']
}) %}


This means your current code should probably be refactored to look something like this (simplified):

{% for article in entry.relatedArticles %}
    ...
    {% set bannerImage = article.bannerImage.find({
        withTransforms: ['cardImage']
    })[0] ?? null %}
    {% if bannerImage %}
        &lt;img src="{{ bannerImage.getUrl('cardImage') }}" /&gt;
    {% endif %}
    ...
{% endfor %}


Assuming there's not a ton of relatedArticles entries, and considering the fact that there's only one image per entry, the above might not make a huge impact on your performance. To take things a step further, you might consider eager loading the actual Asset field(s) as well as their transforms. There's a couple of ways to do this – you could use the same approach as above, adding the eager loading criteria to the Entries field relatedArticles:

{% set relatedArticles = entry.relatedArticles.find({
  with: [
      ['bannerImage', {
          withTransforms: ['cardImage']
      }]
  ]
}) %}
{% for article in relatedArticles %}
    ...
    {% set bannerImage = article.bannerImage[0] ?? null %}
    {% if bannerImage %}
        &lt;img src="{{ bannerImage.getUrl('cardImage') }}" /&gt;
    {% endif %}
{% endfor %}


...or you could use a more "traditional" eager loading approach, using craft.entries and the relatedTo parameter:

{% for article in craft.entries({
    relatedTo: {
      sourceElement: entry,
      field: 'relatedArticles'
    },
    with: [
        ['bannerImage', {
            withTransforms: ['cardImage']
        }]
    ]
}) %}
    ...
    {% set bannerImage = article.bannerImage[0] ?? null %}
    {% if bannerImage %}
        &lt;img src="{{ bannerImage.getUrl('cardImage') }}" /&gt;
    {% endif %}
    ...
{% endfor %}


With the above, assuming there's a fair amount of entries involved you'll probably see a decrease in the number of queries executed.

If you want to learn more about eager loading, I recommend taking a look at the official docs on the subject.