<p>First of all just a few ideas about best practices</p>

<h3>Don't</h3>

<pre class="lang-twig prettyprint-override"><code>{{ entry.relatedBrand.one.url }}
{{ entry.relatedBrand.one.title }}
{{ entry.relatedBrand.one.size }}
</code></pre>

<h3>Do</h3>

<pre class="lang-twig prettyprint-override"><code>{% set relatedImage = entry.relatedBrand.one() %}
{{ relatedImage.url }}
{{ relatedImage.title }}
{{ relatedImage.size }}
</code></pre>

<p>Furthermore your code with eager loading</p>

<pre class="lang-twig prettyprint-override"><code>{% set brandIds = craft
    .entries()
    .section('brands')
    .relatedTo({ targetElement: category })
    .ids() 
%}
{% set offers = craft
    .entries()
    .section('offers')
    .with(['relatedBrand.brandLogo'])
    .relatedTo(brandIds)
    .limit(6) 
    .all()
%}

{% for entry in offers %}
  &lt;div class="card category-card"&gt;
    {% set relatedBrand = entry.relatedBrand? entry.relatedBrand[0] : null %}
    &lt;a href="{{ relatedBrand.url?? '' }}#{{ entry.id }}" title="{{ entry.title }}"&gt;
    &lt;div class="row"&gt;
      &lt;div class="col-md-12 col-4"&gt;
          {% set logos = relatedBrand? relatedBrand.brandLogo : [] %}
          {% for asset in logos %}
            &lt;img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap"&gt;
          {% endfor %}
      &lt;/div&gt;
      &lt;div class="col-md-12 col-8"&gt;
        &lt;div class="card-body"&gt;
          &lt;h5 class="card-title"&gt;{{ entry.title }}&lt;/h5&gt;
          &lt;p&gt;{{ entry.body }}&lt;/p&gt;
          &lt;small class="text-muted"&gt;{{ relatedBrand.title?? '' }}&lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;/div&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-10-29T14:28:02.453Z
Robin! You're amazing! I literally tried 15 things on how to increase the speed of this!!! This has literally nearly halved the load on that query!
JMKelley
CC BY-SA 4.0
2018-10-29T11:52:16.103Z
2018-10-29T11:52:16.103Z
Robin Schambach
<p>I have quite a substantial amount of queries heading out for this bit of code below. I've tried quite a few things to get it down, but I need to eager load some things just to try and reduce the overhead of this, but I still think it won't do enough. Is there anything I can look at to optimise this?</p>

<pre><code>    {% set brandIds = craft.entries().section('brands').relatedTo({ targetElement: category }).ids() %}
    {% set offers = craft.entries().section('offers').relatedTo(brandIds).limit(6) %}

    {% for entry in offers %}
      &lt;div class="card category-card"&gt;
        &lt;a href="{{ entry.relatedBrand.one.url }}#{{ entry.id }}" title="{{ entry.title }}"&gt;
        &lt;div class="row"&gt;
          &lt;div class="col-md-12 col-4"&gt;
              {% for asset in entry.relatedBrand.one.brandLogo.all() %}
                &lt;img class="card-img-top" src="{{ asset.geturl }}" alt="Card image cap"&gt;
              {% endfor %}
          &lt;/div&gt;
          &lt;div class="col-md-12 col-8"&gt;
            &lt;div class="card-body"&gt;
              &lt;h5 class="card-title"&gt;{{ entry.title }}&lt;/h5&gt;
              &lt;p&gt;{{ entry.body }}&lt;/p&gt;
              &lt;small class="text-muted"&gt;{{ entry.relatedBrand.one }}&lt;/small&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;/div&gt;
        &lt;/a&gt;
      &lt;/div&gt;
  {% endfor %}
</code></pre>

<templating><eager-loading>
Eager Loading Relatedto & Target Elements
# Eager Loading Relatedto & Target Elements

(Asked by: username_0 on 2018-10-28)

I have quite a substantial amount of queries heading out for this bit of code below. I've tried quite a few things to get it down, but I need to eager load some things just to try and reduce the overhead of this, but I still think it won't do enough. Is there anything I can look at to optimise this?
    {% set brandIds = craft.entries().section('brands').relatedTo({ targetElement: category }).ids() %}
    {% set offers = craft.entries().section('offers').relatedTo(brandIds).limit(6) %}

    {% for entry in offers %}
      
        
        
          
              {% for asset in entry.relatedBrand.one.brandLogo.all() %}
                
              {% endfor %}
          
          
            
              {{ entry.title }}
              {{ entry.body }}
              {{ entry.relatedBrand.one }}
            
          
          
        
      
  {% endfor %}

**Tags:** templating, eager-loading


---

## 1 answers

### Answer by username_1 on 2018-10-29. Score: 2 | Accepted answer

First of all just a few ideas about best practices
Don't
{{ entry.relatedBrand.one.url }}
{{ entry.relatedBrand.one.title }}
{{ entry.relatedBrand.one.size }}

Do
{% set relatedImage = entry.relatedBrand.one() %}
{{ relatedImage.url }}
{{ relatedImage.title }}
{{ relatedImage.size }}

Furthermore your code with eager loading
{% set brandIds = craft
    .entries()
    .section('brands')
    .relatedTo({ targetElement: category })
    .ids() 
%}
{% set offers = craft
    .entries()
    .section('offers')
    .with(['relatedBrand.brandLogo'])
    .relatedTo(brandIds)
    .limit(6) 
    .all()
%}

{% for entry in offers %}
  
    {% set relatedBrand = entry.relatedBrand? entry.relatedBrand[0] : null %}
    
    
      
          {% set logos = relatedBrand? relatedBrand.brandLogo : [] %}
          {% for asset in logos %}
            
          {% endfor %}
      
      
        
          {{ entry.title }}
          {{ entry.body }}
          {{ relatedBrand.title?? '' }}
        
      
      
    
  
{% endfor %}

> **Comments:**
> **username_0**: Robin! You're amazing! I literally tried 15 things on how to increase the speed of this!!! This has literally nearly halved the load on that query!  

---

