<p><code>.last</code> is the opposite of <code>.first</code>, meaning that you can only apply it at the <em>end</em> of your query.</p>

<pre><code>{% set markets = craft.categories.group('markets').relatedTo(entries).last() %}
</code></pre>

<p>And similar to <code>.first</code>, the <code>.last</code> method will only return a single Entry.</p>

CC BY-SA 3.0
2017-03-01T20:33:52.780Z
That created a template error. `Impossible to access an attribute ("title") on a string variable ("8")`  So, is there a way to do it without using last()?  The goal is to pull all the markets using their last level to create filter buttons.
Hellyar
CC BY-SA 3.0
2017-03-02T19:22:01.840Z
So, I am not trying to return a single entry or any entries.  See above.
Hellyar
CC BY-SA 3.0
2017-03-01T20:13:34.050Z
2017-03-01T20:13:34.050Z
Lindsey D
<p>This allows me to pull the related categories from a second category group for a given category.  </p>

<pre><code>{% set entries = craft.entries.relatedTo(category).find() %}
{% set tags = craft.tags.relatedTo(entries).find() %}   
{% set markets = craft.categories.group('markets').relatedTo(entries).find() %}
</code></pre>

<p><strong>BUT</strong>, it pulls both the parent and descendant categories from markets.  And, some markets do not have descendants - so adding `.level(2) would not capture those that only have a level(1) assigned.  </p>

<p><strong>I want to list only the last level assigned whether it is level(1) or level(2).</strong></p>

<p>I tried adding <code>.last()</code> but that generates a <code>CException</code> error.  </p>

<pre><code>{% set markets = craft.categories.group('markets').last().relatedTo(entries).find() %}
</code></pre>

<p>What am I missing?</p>

<p>Thanks</p>

CC BY-SA 3.0
2017-03-01T20:03:55.247Z
All entries have a category from markets assigned.  It is mandatory.
Hellyar
<templating>
How do I pull the last level of a category?
# How do I pull the last level of a category?

(Asked by: username_0 on 2017-03-01)

This allows me to pull the related categories from a second category group for a given category.  
{% set entries = craft.entries.relatedTo(category).find() %}
{% set tags = craft.tags.relatedTo(entries).find() %}   
{% set markets = craft.categories.group('markets').relatedTo(entries).find() %}

BUT, it pulls both the parent and descendant categories from markets.  And, some markets do not have descendants - so adding `.level(2) would not capture those that only have a level(1) assigned.  
I want to list only the last level assigned whether it is level(1) or level(2).
I tried adding .last() but that generates a CException error.  
{% set markets = craft.categories.group('markets').last().relatedTo(entries).find() %}

What am I missing?
Thanks

**Tags:** templating

> **Comments:**
> **username_0**: All entries have a category from markets assigned.  It is mandatory.  

---

## 1 answers

### Answer by username_1 on 2017-03-01. Score: 1

.last is the opposite of .first, meaning that you can only apply it at the end of your query.
{% set markets = craft.categories.group('markets').relatedTo(entries).last() %}

And similar to .first, the .last method will only return a single Entry.

> **Comments:**
> **username_0**: That created a template error. `Impossible to access an attribute ("title") on a string variable ("8")`  So, is there a way to do it without using last()?  The goal is to pull all the markets using their last level to create filter buttons.  
> **username_0**: So, I am not trying to return a single entry or any entries.  See above.  

---

