<p>No you can't with any build in Craft functions. I've added <a href="http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7203963-eager-loading-of-field-relations-in-templates" rel="nofollow">a feature request for this on Crafts feedback site</a>. </p>

<p>What you can do for now is to use Mike Peppers excellent Plugin "Fetch" which pre-fetch relations for you. </p>

<p>Ser here <a href="https://github.com/mdcpepper/fetch" rel="nofollow">https://github.com/mdcpepper/fetch</a></p>

CC BY-SA 3.0
2015-04-18T05:08:46.460Z
2015-04-18T10:42:57.443Z
2015-04-18T10:42:57.443Z
naboovalley
<p>I have a Matrix field with a single block type and within that, a number of fields that I need to pull out:</p>

<pre><code>{% for roomContent in hotelEntry.roomsAvailable %}
    {% if roomContent.code == room.roomTypeCode %}
        &lt;p&gt;room code = {{ roomContent.code }}&lt;br&gt;
            description = {{ roomContent.description }}&lt;br&gt;
            {% for image in roomContent.images %}
                &lt;img src="{{ image.url }}" alt="{{ roomContent.description }}" height="50"&gt;
            {% endfor %}
        &lt;/p&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>I am doing this on a listing page where there are around 60 entries, so this is resulting in a lot of calls to the DB.</p>

<p>Is there a way I can eager load all of the data for all of the entries outside of this loop so I'm not recursively calling for data?</p>

<matrix>
Can I eager load data within a template?
# Can I eager load data within a template?

(Asked by: username_0 on 2015-04-17)

I have a Matrix field with a single block type and within that, a number of fields that I need to pull out:
{% for roomContent in hotelEntry.roomsAvailable %}
    {% if roomContent.code == room.roomTypeCode %}
        room code = {{ roomContent.code }}
            description = {{ roomContent.description }}
            {% for image in roomContent.images %}
                
            {% endfor %}
        
    {% endif %}
{% endfor %}

I am doing this on a listing page where there are around 60 entries, so this is resulting in a lot of calls to the DB.
Is there a way I can eager load all of the data for all of the entries outside of this loop so I'm not recursively calling for data?

**Tags:** matrix

---

## 1 answers

### Answer by username_1 on 2015-04-18. Score: 1 | Accepted answer

No you can't with any build in Craft functions. I've added [a feature request for this on Crafts feedback site](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7203963-eager-loading-of-field-relations-in-templates).

What you can do for now is to use Mike Peppers excellent Plugin "Fetch" which pre-fetch relations for you.

Ser here <https://github.com/mdcpepper/fetch>


---

