<p>Whether you use <a href="https://laravel.com/docs/9.x/collections#method-groupby" rel="nofollow noreferrer">groupBy</a> (for Collections, Craft 4+ only) or Craft's <a href="https://craftcms.com/docs/4.x/dev/filters.html#group" rel="nofollow noreferrer">group filter</a> (for arrays), you'll get back an array where the keys are the thing you grouped by, and the values are nested arrays for each group. Therefore you'll need a nested loop to access your data. In a Twig loop, you can address the key and value separately like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set entriesGrouped = craft.entries().collect().groupBy('section.handle') %}

... alternatively without using collections, you could use the group filter like this:

{% set entriesGrouped = craft.entries().all()|group(entry =&gt; entry.section.handle) %}

{% for section, entries in entriesGrouped %}
    &lt;h3&gt;{{ section }}&lt;/h3&gt;
    &lt;ul&gt;
        {% for entry in entries %}
            &lt;li&gt;{{ entry }}&lt;/li&gt;      
        {% endfor %}
    &lt;/ul&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2023-01-22T01:45:58.117Z
Thank you! As expected, a silly one from me.
supazu
CC BY-SA 4.0
2023-01-21T13:34:34.097Z
2023-01-23T09:03:39.607Z
2023-01-23T09:03:39.607Z
James Smith
<p>This feels like a really dumb one...when I'm grouping a collection, how do I loop over it?</p>
<pre><code>{% set entries = craft.entries().collect() %}
{% set groupBySection = entries.groupBy('section.handle') %}
{% for entry in groupBySection %}
    {{ entry.title }}
{% endfor %}
</code></pre>
<p>I get nothing back. What am I missing?</p>

<elementquery>
Collections groupBy
# Collections groupBy

(Asked by: username_0 on 2023-01-21)

This feels like a really dumb one...when I'm grouping a collection, how do I loop over it?
{% set entries = craft.entries().collect() %}
{% set groupBySection = entries.groupBy('section.handle') %}
{% for entry in groupBySection %}
    {{ entry.title }}
{% endfor %}

I get nothing back. What am I missing?

**Tags:** elementquery


---

## 1 answers

### Answer by username_1 on 2023-01-21. Score: 3 | Accepted answer

Whether you use groupBy (for Collections, Craft 4+ only) or Craft's group filter (for arrays), you'll get back an array where the keys are the thing you grouped by, and the values are nested arrays for each group. Therefore you'll need a nested loop to access your data. In a Twig loop, you can address the key and value separately like this:
{% set entriesGrouped = craft.entries().collect().groupBy('section.handle') %}

... alternatively without using collections, you could use the group filter like this:

{% set entriesGrouped = craft.entries().all()|group(entry => entry.section.handle) %}

{% for section, entries in entriesGrouped %}
    {{ section }}
    
        {% for entry in entries %}
            {{ entry }}      
        {% endfor %}
    
{% endfor %}

> **Comments:**
> **username_0**: Thank you! As expected, a silly one from me.  

---

