Answers.Body: <p>To use eager-loading for the child elements add <code>'children'</code> to the <code>with</code> parameter.</p>
  
  <pre><code>{% set mice = craft.entries({
      section: 'experiments',
      level: 3,
      descendantOf: entry.id,
      with: ['children', {order: 'postDate asc'}],
  }) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-14T10:18:06.230Z
Answers.LastActivityDate: 2018-02-14T10:18:06.230Z
Answers.UserDisplayName: carlcs
Body: <p>I have a structure that looks like this:</p>
  
  <ul>
  <li><p>Experiment ( Level 1)</p>
  
  <ul>
  <li>Cages (Level 2)</li>
  <li>Animals (Level 3)</li>
  <li>Measurement (Level 4)</li>
  </ul></li>
  </ul>
  
  <p>I am outputting this information into some inline JSON like so: </p>
  
  <pre><code>{% set mice = craft.entries({
      'section' : 'experiments',
      'level' : 3,
      'descendantOf' : entry.id,
  }) %}
  
  &lt;script&gt;
    var data = [
      {% for mouse in mice %}
      {
        "Cage": "{{ mouse.parent.title }}",
        "Mouse": "{{ mouse.title }}",
        "measurements": [
          {% for data in mouse.children.order('postDate') %}
          {
            "Date": "{{ data.postDate| date("d-M") }}",
            "Weight(g)": "{{ data.weight }}",
            "Length": "{{ data.length }}",
            "Depth": "{{ data.depth }}",
            "Tumor Volume": {{ data.tumourVolume }}
          }{% if not loop.last %}, {% endif %}
          {% endfor %}
        ]
      }{% if not loop.last %}, {% endif %}
      {% endfor %}
    ];
  &lt;/script&gt;
  </code></pre>
  
  <p>This is ok until entries start to increase, the TTFB begins to add up significantly - I am wondering if there is a way to eager load this sort of structure? The entries are very dynamic so caching isn't an option.</p>
  
  <p>Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-13T13:34:56.643Z
Comments.Text: Have you every read this documentation https://craftcms.com/docs/templating/eager-loading-elements#eager-loading-nested-sets-of-elements or heard about the element api https://github.com/craftcms/element-api that returns ready to use transformed json? It does not really make sense to re-explain the docs just because you had not read them
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-13T13:43:42.410Z
Comments.Text: Hi Robin — thanks for the reply, I have and I tried using the with: 'dependents' but it throws an array error. I looked around for the correct syntax but had no luck. I am using element API already but I thought it would be easier to just eager load the entries if it possible possible first instead of creating an end point.
Comments.UserDisplayName: Graham Winston
Tags: <structure><eager-loading><children>
Title: Eager-Loading child elements in a multi-level structure section
ThreadText: # Eager-Loading child elements in a multi-level structure section
  
  (Asked by: username_0 on 2018-02-13)
  
  I have a structure that looks like this:
  
  * Experiment ( Level 1)
  
    + Cages (Level 2)
    + Animals (Level 3)
    + Measurement (Level 4)
  
  I am outputting this information into some inline JSON like so:
  
  ```
  {% set mice = craft.entries({
      'section' : 'experiments',
      'level' : 3,
      'descendantOf' : entry.id,
  }) %}
  
  <script>
    var data = [
      {% for mouse in mice %}
      {
        "Cage": "{{ mouse.parent.title }}",
        "Mouse": "{{ mouse.title }}",
        "measurements": [
          {% for data in mouse.children.order('postDate') %}
          {
            "Date": "{{ data.postDate| date("d-M") }}",
            "Weight(g)": "{{ data.weight }}",
            "Length": "{{ data.length }}",
            "Depth": "{{ data.depth }}",
            "Tumor Volume": {{ data.tumourVolume }}
          }{% if not loop.last %}, {% endif %}
          {% endfor %}
        ]
      }{% if not loop.last %}, {% endif %}
      {% endfor %}
    ];
  </script>
  
  ```
  
  This is ok until entries start to increase, the TTFB begins to add up significantly - I am wondering if there is a way to eager load this sort of structure? The entries are very dynamic so caching isn't an option.
  
  Thanks!
  
  **Tags:** structure, eager-loading, children
  
  > **Comments:**
  > **username_1**: Have you every read this documentation https://craftcms.com/docs/templating/eager-loading-elements#eager-loading-nested-sets-of-elements or heard about the element api https://github.com/craftcms/element-api that returns ready to use transformed json? It does not really make sense to re-explain the docs just because you had not read them  
  > **username_0**: Hi username_1 — thanks for the reply, I have and I tried using the with: 'dependents' but it throws an array error. I looked around for the correct syntax but had no luck. I am using element API already but I thought it would be easier to just eager load the entries if it possible possible first instead of creating an end point.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-02-14. Score: 3 | Accepted answer
  
  To use eager-loading for the child elements add `'children'` to the `with` parameter.
  
  ```
  {% set mice = craft.entries({
      section: 'experiments',
      level: 3,
      descendantOf: entry.id,
      with: ['children', {order: 'postDate asc'}],
  }) %}
  
  ```
  
  
  ---
  
  