Answers.Body: <p>The issue is likely this line:</p>
  <pre><code>{% set itemImages = itemImages|merge(itemImage.relatedImages) %}
  </code></pre>
  <p>The <code>|merge</code> filter treats the <code>relatedImages</code> field as an array, which is why you get that deprecation error.</p>
  <p>So to fix it, add the <code>.all()</code> call to execute the query (which will return an array), before merging:</p>
  <pre><code>{% set itemImages = itemImages|merge(itemImage.relatedImages.all()) %}
  </code></pre>
  <p>A tip that could make it easier to track down issues like these in the future, is to enable <a href="https://craftcms.com/knowledge-base/what-dev-mode-does" rel="nofollow noreferrer"><code>devMode</code></a>, and add this to your <code>config/app.php</code> config file:</p>
  <pre class="lang-php prettyprint-override"><code>return [
      'components' =&gt; [
          'deprecator' =&gt; [
              'throwExceptions' =&gt; YII_DEBUG,
          ],
      ],
  ];
  </code></pre>
  <p>That will make Craft throw an actual PHP exception for deprecated code, with a full stack trace (instead of just logging the deprecation), which can make it easier to debug this stuff.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-25T17:37:27.037Z
Answers.Text: Awesome thanks! Works like a charm. I've enabled the deprecator in my config. It was really tough to find which templates were causing the issue in the stack trace before.
Answers.UserDisplayName: Yawner
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-25T17:26:36.157Z
Answers.LastActivityDate: 2022-03-25T17:32:15.977Z
Answers.LastEditDate: 2022-03-25T17:32:15.977Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>This is very nearly a duplicate question but I can't figure out where to insert <a href="https://mijingo.com/lessons/using-dot-all-in-craft-3-element-queries/" rel="nofollow noreferrer"><code>.all()</code></a> to execute my <a href="https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries" rel="nofollow noreferrer">element query</a>. I'm still asking because I may be messing up php, function or filter syntax somewhere. I've looked through other related answers (<a href="https://craftcms.stackexchange.com/questions/29366/output-deprecation-error">1</a>, <a href="https://craftcms.stackexchange.com/questions/35244/looping-through-element-queries-directly-has-been-deprecated">2</a>).</p>
  <p>I traced the error to the template(s) below. The first <code>_entry.twig</code> chooses a layout based on url parameters and Admin CP settings:</p>
  <pre><code>{% block content %}
    {% if craft.app.request.segments|length == 3 %}
      {% set entry = craft.entries().slug(slug|literal).one() %}
      {% include &quot;shared/_grid-item&quot; with {
        entry: entry,
        index: number,
      } only %}
  
    {% elseif entry.galleryShowThumbnailPage|length %}
      {% include &quot;shared/_grid&quot; with { entry: entry } %}
  
    {% else %}
      {% include &quot;shared/_gallery&quot; with { entry: entry } %}
    {% endif %}
  {% endblock %}
  </code></pre>
  <p>I'm fairly sure the error is <strong>not</strong> coming from <code>_entry.twig</code> but may be caused by the variable <code>entry</code> in the <code>shared/_grid-item.twig</code> partial it gets passed to:</p>
  <pre><code>{% set images = entry.galleryImages.withTransforms(['sideScrollImage', 'tinyThumb']).all() %}
  {% set itemImage = images[index - 1] %}
  {% set itemImages = []|merge([itemImage]) %}
  {% if itemImage.relatedImages.exists() %}
    {% set itemImages = itemImages|merge(itemImage.relatedImages) %}
  {% endif %}
  
  {% set next = entry.url ~ '/' ~ (index + 1) %}
  {% set prev = entry.url ~ '/' ~ (index - 1) %}
  {% if index == 1 %}{% set prev = false %}{% endif %}
  {% if index == (images|length) %}{% set next = false %}{% endif %}
  
  {% extends &quot;_layouts/_default.twig&quot; %}
  
  {% block content %}
      &lt;main role=&quot;main&quot; class=&quot;content&quot;&gt;
        &lt;h1 class=&quot;content__title content__title--hidden&quot;&gt;
          {{ entry.title }}
        &lt;/h1&gt;
  
        {% include &quot;_partials/_sideScroll&quot; with {
          back: entry.title,
          images: itemImages,
          next: next,
          prev: prev,
        } only %}
      &lt;/main&gt;
    {% cache %}
    {% endcache %}
  {% endblock %}
  </code></pre>
  <p>My understanding of the element query is that I call a for loop but then execute it by calling <code>.all()</code>. I'm not sure if any of the code in <code>_grid-item.twig</code> would call a for loop I'm unaware of – maybe in
  <code>{% set itemImages = []|merge([itemImage]) %}</code>
  or
  <code>{% set itemImages = itemImages|merge(itemImage.relatedImages) %}</code>?
  Does some of the php syntax need to change to allow adding <code>.all()</code>?</p>
  <p>I've tried adding <code>.all()</code> various places like <code>{% set itemImage = images.all()[index - 1] %}</code> but everything throws a twig runtime error.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-03-25T17:22:23.300Z
Comments.Text: What kind of field is “entry.galleryShowThumbnailPage”?
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-03-25T17:24:50.883Z
Comments.Text: `entry.galleryShowThumbnailPage` is a checkbox field type
Comments.UserDisplayName: Yawner
Tags: <craft3><templating><php>
Title: .all() — Looping through element queries directly has been deprecated
ThreadText: # .all() — Looping through element queries directly has been deprecated
  
  (Asked by: username_0 on 2022-03-25)
  
  This is very nearly a duplicate question but I can't figure out where to insert [`.all()`](https://mijingo.com/lessons/using-dot-all-in-craft-3-element-queries/) to execute my [element query](https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries). I'm still asking because I may be messing up php, function or filter syntax somewhere. I've looked through other related answers ([1](https://craftcms.stackexchange.com/questions/29366/output-deprecation-error), [2](https://craftcms.stackexchange.com/questions/35244/looping-through-element-queries-directly-has-been-deprecated)).
  
  I traced the error to the template(s) below. The first `_entry.twig` chooses a layout based on url parameters and Admin CP settings:
  
  ```
  {% block content %}
    {% if craft.app.request.segments|length == 3 %}
      {% set entry = craft.entries().slug(slug|literal).one() %}
      {% include "shared/_grid-item" with {
        entry: entry,
        index: number,
      } only %}
  
    {% elseif entry.galleryShowThumbnailPage|length %}
      {% include "shared/_grid" with { entry: entry } %}
  
    {% else %}
      {% include "shared/_gallery" with { entry: entry } %}
    {% endif %}
  {% endblock %}
  
  ```
  
  I'm fairly sure the error is **not** coming from `_entry.twig` but may be caused by the variable `entry` in the `shared/_grid-item.twig` partial it gets passed to:
  
  ```
  {% set images = entry.galleryImages.withTransforms(['sideScrollImage', 'tinyThumb']).all() %}
  {% set itemImage = images[index - 1] %}
  {% set itemImages = []|merge([itemImage]) %}
  {% if itemImage.relatedImages.exists() %}
    {% set itemImages = itemImages|merge(itemImage.relatedImages) %}
  {% endif %}
  
  {% set next = entry.url ~ '/' ~ (index + 1) %}
  {% set prev = entry.url ~ '/' ~ (index - 1) %}
  {% if index == 1 %}{% set prev = false %}{% endif %}
  {% if index == (images|length) %}{% set next = false %}{% endif %}
  
  {% extends "_layouts/_default.twig" %}
  
  {% block content %}
      <main role="main" class="content">
        <h1 class="content__title content__title--hidden">
          {{ entry.title }}
        </h1>
  
        {% include "_partials/_sideScroll" with {
          back: entry.title,
          images: itemImages,
          next: next,
          prev: prev,
        } only %}
      </main>
    {% cache %}
    {% endcache %}
  {% endblock %}
  
  ```
  
  My understanding of the element query is that I call a for loop but then execute it by calling `.all()`. I'm not sure if any of the code in `_grid-item.twig` would call a for loop I'm unaware of – maybe in
  `{% set itemImages = []|merge([itemImage]) %}`
  or
  `{% set itemImages = itemImages|merge(itemImage.relatedImages) %}`?
  Does some of the php syntax need to change to allow adding `.all()`?
  
  I've tried adding `.all()` various places like `{% set itemImage = images.all()[index - 1] %}` but everything throws a twig runtime error.
  
  **Tags:** craft3, templating, php
  
  > **Comments:**
  > **username_1**: What kind of field is “entry.galleryShowThumbnailPage”?  
  > **username_0**: `entry.galleryShowThumbnailPage` is a checkbox field type  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-03-25. Score: 3 | Accepted answer
  
  The issue is likely this line:
  
  ```
  {% set itemImages = itemImages|merge(itemImage.relatedImages) %}
  
  ```
  
  The `|merge` filter treats the `relatedImages` field as an array, which is why you get that deprecation error.
  
  So to fix it, add the `.all()` call to execute the query (which will return an array), before merging:
  
  ```
  {% set itemImages = itemImages|merge(itemImage.relatedImages.all()) %}
  
  ```
  
  A tip that could make it easier to track down issues like these in the future, is to enable [`devMode`](https://craftcms.com/knowledge-base/what-dev-mode-does), and add this to your `config/app.php` config file:
  
  ```php
  return [
      'components' => [
          'deprecator' => [
              'throwExceptions' => YII_DEBUG,
          ],
      ],
  ];
  
  ```
  
  That will make Craft throw an actual PHP exception for deprecated code, with a full stack trace (instead of just logging the deprecation), which can make it easier to debug this stuff.
  
  > **Comments:**
  > **username_0**: Awesome thanks! Works like a charm. I've enabled the deprecator in my config. It was really tough to find which templates were causing the issue in the stack trace before.  
  
  ---
  
  