<p>Make sure to add <code>.all()</code> to the end of your <code>craft.entries</code> element query, which will execute it and return the results as a simple array:</p>
<pre><code>{% set entry = craft.entries.relatedTo(asset).orderBy('title').all() %}
{% for entry in entry %}
   &lt;a href=&quot;{{ entry.url }}&quot;&gt;{{ entry.title }}&lt;/a&gt;
   {{ not loop.last ? ' &amp;amp; ' }} 
{% endfor %}
</code></pre>
<p>The reason why you're getting the error without <code>.all()</code> is that the <code>loop.last</code> variable is only added to the Twig context within a <code>{% for %}</code> loop if <em>Twig can reliably tell how many things it's looping over</em>. Looping on element queries (i.e. the thing returned by <code>craft.entries</code>) directly is discouraged for a number of reasons, this gotcha being one of them.</p>

CC BY-SA 4.0
2024-02-18T13:45:58.183Z
Thanks Mats, This was a template in a Craft 2 site that I upgraded to Craft 4 and I missed the significance of .all() in this query as it was all working fine in production mode. It was as only when testing something else in Dev mode it showed up. I expected this sort of thing to show up in the 'Depreciation Warnings'
Paul Frost
CC BY-SA 4.0
2024-02-18T12:10:18.900Z
2024-02-18T12:10:18.900Z
Mats Mikkel Rummelhoff
<p>In Dev mode I get the following runtime error:</p>
<pre><code>Key &quot;last&quot; for array with keys &quot;parent, index0, index, first&quot; does not exist
</code></pre>
<p>and it highlights the line {{ not loop.last ? ' &amp; ' }}</p>
<p>In Production mode the template works fine.
This is the code:</p>
<pre><code>{# Loop through entries related to an asset #}
{% set entry = craft.entries.relatedTo(asset).orderby('title') %}
    {% for entry in entry %}
       &lt;a href=&quot;{{ entry.url }}&quot;&gt;{{ entry.title }}&lt;/a&gt;
       {{ not loop.last ? ' &amp;amp; ' }} 
    {% endfor %}
</code></pre>

<templating><loop>
Problem with loop.last
# Problem with loop.last

(Asked by: username_0 on 2024-02-18)

In Dev mode I get the following runtime error:
Key "last" for array with keys "parent, index0, index, first" does not exist

and it highlights the line {{ not loop.last ? ' & ' }}
In Production mode the template works fine.
This is the code:
{# Loop through entries related to an asset #}
{% set entry = craft.entries.relatedTo(asset).orderby('title') %}
    {% for entry in entry %}
       {{ entry.title }}
       {{ not loop.last ? ' & ' }} 
    {% endfor %}

**Tags:** templating, loop


---

## 1 answers

### Answer by username_1 on 2024-02-18. Score: 2 | Accepted answer

Make sure to add .all() to the end of your craft.entries element query, which will execute it and return the results as a simple array:
{% set entry = craft.entries.relatedTo(asset).orderBy('title').all() %}
{% for entry in entry %}
   {{ entry.title }}
   {{ not loop.last ? ' & ' }} 
{% endfor %}

The reason why you're getting the error without .all() is that the loop.last variable is only added to the Twig context within a {% for %} loop if Twig can reliably tell how many things it's looping over. Looping on element queries (i.e. the thing returned by craft.entries) directly is discouraged for a number of reasons, this gotcha being one of them.

> **Comments:**
> **username_0**: Thanks Mats, This was a template in a Craft 2 site that I upgraded to Craft 4 and I missed the significance of .all() in this query as it was all working fine in production mode. It was as only when testing something else in Dev mode it showed up. I expected this sort of thing to show up in the 'Depreciation Warnings'  

---

