Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-22T01:45:58.117Z
Answers.Text: Thank you! As expected, a silly one from me.
Answers.UserDisplayName: supazu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-21T13:34:34.097Z
Answers.LastActivityDate: 2023-01-23T09:03:39.607Z
Answers.LastEditDate: 2023-01-23T09:03:39.607Z
Answers.UserDisplayName: James Smith
Body: <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>
  
Tags: <elementquery>
Title: Collections groupBy
ThreadText: # 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](https://laravel.com/docs/9.x/collections#method-groupby) (for Collections, Craft 4+ only) or Craft's [group filter](https://craftcms.com/docs/4.x/dev/filters.html#group) (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:
  
  ```twig
  {% 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 %}
      <h3>{{ section }}</h3>
      <ul>
          {% for entry in entries %}
              <li>{{ entry }}</li>      
          {% endfor %}
      </ul>
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you! As expected, a silly one from me.  
  
  ---
  
  