<p><code>with</code> is just another query param, like <code>limit</code> or whatever parameters you’re defining in <code>entryParams</code>.</p>

<p>The <code>{% paginate %}</code> tag requires that you pass in an ElementCriteriaModel, but as soon as you call <code>find()</code> you’re actually dealing with an array of the matched entries.</p>

<p>So the key to getting eager-loading working with pagination is to assign the <code>with</code> param like any other param, rather than passing it into the find() function.</p>

<pre><code>{% set queryParams = {
    // ...
    with: [
        'gridMatrixBlock1:rowItem1',
        ['gridMatrixBlock1:rowItem1.gridimageFile', {
            kind: 'image',
            withTransforms: projectThumb
        }],
        ...
    ]
} %}
</code></pre>

<p>and then you can create your ElementCriteriaModel just like you were doing before:</p>

<pre><code>{% set allProjects = craft.entries(queryParams).limit(itemsPerPage) %}
</code></pre>

CC BY-SA 3.0
2016-03-31T10:19:44.857Z
That's perfect Brandon! I didn't know we could also do it like this (which I personally prefer). Thanks!
Pierre Stoffe
CC BY-SA 3.0
2016-03-30T20:35:19.187Z
2016-03-30T20:35:19.187Z
Brandon Kelly
<p>I'm currently working on a relation-heavy website with a colleague of mine and, for that reason, eager-loading stuff looks very appealing to us.
We've succeeded in eager-loading pretty much everything that we needed but there is just one kind of template that we couldn't make it work with: paginated archives</p>

<p><strong>The context:</strong>
We have this archive page that loads every entry of a 'projects' section and, next to each entry, we have a list of all images that the entry contains which is between 8-20 images (remember, I said it was relation-heavy ;) ). We care a lot about performance so we decided to paginate this big entry listing.</p>

<p><strong>The issue:</strong>
All this works very well except that when we tried to eager-load those images, we transformed our original query which was:</p>

<pre><code>{% set allProjects = craft.entries(queryParams).limit(itemsPerPage) %}
</code></pre>

<p>to</p>

<pre><code>{% set allProjects = craft.entries(queryParams).limit(itemsPerPage).find({
    with: [
        'gridMatrixBlock1:rowItem1',
        ['gridMatrixBlock1:rowItem1.gridimageFile', {
            kind: 'image',
            withTransforms: projectThumb
        }],
        ...
    ]
}) %}
</code></pre>

<p><strong>The question:</strong>
Since .find() returns an array, we have no way to paginate this (pagination need an ElementCriteriaModel). Is there any workaround I don't know about?</p>

<elementcriteriamodel><pagination><array><eager-loading>
Eager-load and pagination
# Eager-load and pagination

(Asked by: username_0 on 2016-03-30)

I'm currently working on a relation-heavy website with a colleague of mine and, for that reason, eager-loading stuff looks very appealing to us.
We've succeeded in eager-loading pretty much everything that we needed but there is just one kind of template that we couldn't make it work with: paginated archives
The context:
We have this archive page that loads every entry of a 'projects' section and, next to each entry, we have a list of all images that the entry contains which is between 8-20 images (remember, I said it was relation-heavy ;) ). We care a lot about performance so we decided to paginate this big entry listing.
The issue:
All this works very well except that when we tried to eager-load those images, we transformed our original query which was:
{% set allProjects = craft.entries(queryParams).limit(itemsPerPage) %}

to
{% set allProjects = craft.entries(queryParams).limit(itemsPerPage).find({
    with: [
        'gridMatrixBlock1:rowItem1',
        ['gridMatrixBlock1:rowItem1.gridimageFile', {
            kind: 'image',
            withTransforms: projectThumb
        }],
        ...
    ]
}) %}

The question:
Since .find() returns an array, we have no way to paginate this (pagination need an ElementCriteriaModel). Is there any workaround I don't know about?

**Tags:** elementcriteriamodel, pagination, array, eager-loading


---

## 1 answers

### Answer by username_1 on 2016-03-30. Score: 3 | Accepted answer

with is just another query param, like limit or whatever parameters you’re defining in entryParams.
The {% paginate %} tag requires that you pass in an ElementCriteriaModel, but as soon as you call find() you’re actually dealing with an array of the matched entries.
So the key to getting eager-loading working with pagination is to assign the with param like any other param, rather than passing it into the find() function.
{% set queryParams = {
    // ...
    with: [
        'gridMatrixBlock1:rowItem1',
        ['gridMatrixBlock1:rowItem1.gridimageFile', {
            kind: 'image',
            withTransforms: projectThumb
        }],
        ...
    ]
} %}

and then you can create your ElementCriteriaModel just like you were doing before:
{% set allProjects = craft.entries(queryParams).limit(itemsPerPage) %}

> **Comments:**
> **username_0**: That's perfect Brandon! I didn't know we could also do it like this (which I personally prefer). Thanks!  

---

