<p>How about stacking all the element query params into a single object?</p>

<pre><code>{% set entries = craft.entries({
    section: 'news',
    order: 'sticky desc, postDate desc',
    limit: 9,
    with: [
        ['newsHighlightImage', {
            withTransforms: ['newsMedium', 'newsSmall']
        }]
    ]
}) %}
</code></pre>

<p>Also, I don't believe you need to use the <code>find()</code> function here. The examples in the documentation only use <code>find()</code> when eager-loading just the image transforms related to a single asset field.</p>

<pre><code>{% set assets = entry.assetsField.find({
    withTransforms: [
        'heroImage',
        { width: 100, height: 100 }
    ]
}) %}
</code></pre>

CC BY-SA 4.0
2019-09-10T02:42:49.683Z
Tried it and it works, thank you. Funny thing is that I was going to say it seems at first that it's exactly like mine but with a different syntax, but dev mode says it run one additional query than mine for some reason…
Willhaus
CC BY-SA 4.0
2019-09-10T05:26:02.167Z
¯\_(ツ)_/¯ Eager loading magic? Who knows. Also magnets... how do they work? ;)
Alex Roper
CC BY-SA 4.0
2019-09-10T00:28:02.810Z
2019-09-10T05:24:14.360Z
2019-09-10T05:24:14.360Z
Alex Roper
<p>I am trying to add eager loading to a query. The original query was as follows (‘sticky’ is a custom lightswitch field for marking sticky entries):</p>

<pre><code>{% set news = craft.entries.section('news').order('sticky desc, postDate desc').limit(9) %}
</code></pre>

<p>Is the following ok syntax for adding eager loading?</p>

<pre><code>{% set news = craft.entries.section('news').order('sticky desc, postDate desc').limit(9).find({
    with: [
        ['newsHighlightImage', {
            withTransforms: ['newsMedium', 'newsSmall']
        }]
    ]
}) %}
</code></pre>

<p>I ask because although the above seems to work, in the examples in <a href="https://docs.craftcms.com/v2/templating/eager-loading-elements.html" rel="nofollow noreferrer">the documentation</a>, the find/with bits come right after the section parameter. But if I do it that way, I get an error about not being able to apply the “order” method. I reckon that’s because find() turns the result into an array so “order” is not longer available. But I couldn’t figure out the correct carry syntax to make it work that way. Is there a better way to do this?</p>

<p>Thanks!</p>

<p>Willhaus </p>

<array><eager-loading>
Question About Eager Loading Syntax
# Question About Eager Loading Syntax

(Asked by: username_0 on 2019-09-09)

I am trying to add eager loading to a query. The original query was as follows (‘sticky’ is a custom lightswitch field for marking sticky entries):
{% set news = craft.entries.section('news').order('sticky desc, postDate desc').limit(9) %}

Is the following ok syntax for adding eager loading?
{% set news = craft.entries.section('news').order('sticky desc, postDate desc').limit(9).find({
    with: [
        ['newsHighlightImage', {
            withTransforms: ['newsMedium', 'newsSmall']
        }]
    ]
}) %}

I ask because although the above seems to work, in the examples in the documentation, the find/with bits come right after the section parameter. But if I do it that way, I get an error about not being able to apply the “order” method. I reckon that’s because find() turns the result into an array so “order” is not longer available. But I couldn’t figure out the correct carry syntax to make it work that way. Is there a better way to do this?
Thanks!
username_0

**Tags:** array, eager-loading


---

## 1 answers

### Answer by username_1 on 2019-09-10. Score: 1

How about stacking all the element query params into a single object?
{% set entries = craft.entries({
    section: 'news',
    order: 'sticky desc, postDate desc',
    limit: 9,
    with: [
        ['newsHighlightImage', {
            withTransforms: ['newsMedium', 'newsSmall']
        }]
    ]
}) %}

Also, I don't believe you need to use the find() function here. The examples in the documentation only use find() when eager-loading just the image transforms related to a single asset field.
{% set assets = entry.assetsField.find({
    withTransforms: [
        'heroImage',
        { width: 100, height: 100 }
    ]
}) %}

> **Comments:**
> **username_0**: Tried it and it works, thank you. Funny thing is that I was going to say it seems at first that it's exactly like mine but with a different syntax, but dev mode says it run one additional query than mine for some reason…  
> **username_1**: ¯\_(ツ)_/¯ Eager loading magic? Who knows. Also magnets... how do they work? ;)  

---

