<p>That <code>documentType</code> in your group filter returns a criteria model prepared to query for categories related to that Matrix block. Use the <code>first()</code> method to try and get the first element matching your criteria and you can now access a property like the category’s title.</p>

<pre><code>{% set grouped = entry.documents|group('documentType.first().title') %}
</code></pre>

<p>But this is also dangerous, as it might happen that a Matrix block does not have a category assigned (may be that this category got deleted). Here’s how you could improve it and write the code a bit more defensively.</p>

<pre><code>{% set grouped = entry.documents|group('{ documentType.first() ? documentType.first().title : 'No categories }') %}
</code></pre>

CC BY-SA 3.0
2016-06-29T11:31:37.490Z
2016-06-29T11:31:37.490Z
carlcs
<p>I'm guessing that you'd like <code>{{ categoryTitle }}</code> to be the current groups <code>documentType.title</code>? </p>

<p>If so, I would output <code>document.documentType</code> for the first document in each group using <code>{% if loop.first %}</code>.</p>

<p>Complete code suggestion would be:</p>

<pre><code>{% set grouped = entry.documents|group('documentType.first().title') %}

{% for groupedRows in grouped %}
  {% for document in groupedRows %}
    {% if loop.first %}
      {{ document.documentType }}
    {% endif %}
    {{ document.documentTitle }}
  {% endfor %}
{% endfor %}
</code></pre>

<p><strong>EDIT:</strong> updated to use <a href="https://craftcms.stackexchange.com/a/15712/4466">carlcs</a> group filter suggestion.</p>

CC BY-SA 3.0
2016-06-29T11:30:28.030Z
2016-06-29T11:58:20.340Z
2017-04-13T12:50:59.930Z
Luke Pearce
<p>I'm looping through a Matrix field called Documents. The matrix has three fields:</p>

<ul>
<li>Document (an asset field)</li>
<li>Document Title (plain text field)</li>
<li>Document Type (a category field)</li>
</ul>

<p>I need to loop through the matrix and output all the rows, but grouped by Document Type. This seems like it should work:</p>

<pre><code>{% set grouped = entry.documents | group('documentType.title') %}

{% for categoryTitle, groupedRows in grouped %}
    TITLE HERE: {{ categoryTitle }}

    {% for document in groupedRows %}
        &lt;br/&gt;{{ document.documentTitle }}
    {% endfor %}
{% endfor %}
</code></pre>

<p>The problem is that <code>{{ categoryTitle }}</code> is blank. Nothing in it. The code runs, but the key is blank. The grouped rows are populated.</p>

<p>What's wrong?</p>

<matrix>
Why does my Twig for loop not get a Key?
# Why does my Twig for loop not get a Key?

(Asked by: username_0 on 2016-06-29)

I'm looping through a Matrix field called Documents. The matrix has three fields:

Document (an asset field)
Document Title (plain text field)
Document Type (a category field)

I need to loop through the matrix and output all the rows, but grouped by Document Type. This seems like it should work:
{% set grouped = entry.documents | group('documentType.title') %}

{% for categoryTitle, groupedRows in grouped %}
    TITLE HERE: {{ categoryTitle }}

    {% for document in groupedRows %}
        {{ document.documentTitle }}
    {% endfor %}
{% endfor %}

The problem is that {{ categoryTitle }} is blank. Nothing in it. The code runs, but the key is blank. The grouped rows are populated.
What's wrong?

**Tags:** matrix


---

## 2 answers

### Answer by username_1 on 2016-06-29. Score: 2 | Accepted answer

That documentType in your group filter returns a criteria model prepared to query for categories related to that Matrix block. Use the first() method to try and get the first element matching your criteria and you can now access a property like the category’s title.
{% set grouped = entry.documents|group('documentType.first().title') %}

But this is also dangerous, as it might happen that a Matrix block does not have a category assigned (may be that this category got deleted). Here’s how you could improve it and write the code a bit more defensively.
{% set grouped = entry.documents|group('{ documentType.first() ? documentType.first().title : 'No categories }') %}


---

### Answer by username_2 on 2016-06-29. Score: 2

I'm guessing that you'd like {{ categoryTitle }} to be the current groups documentType.title? 
If so, I would output document.documentType for the first document in each group using {% if loop.first %}.
Complete code suggestion would be:
{% set grouped = entry.documents|group('documentType.first().title') %}

{% for groupedRows in grouped %}
  {% for document in groupedRows %}
    {% if loop.first %}
      {{ document.documentType }}
    {% endif %}
    {{ document.documentTitle }}
  {% endfor %}
{% endfor %}

EDIT: updated to use username_1 group filter suggestion.


---

