<p>The slower but easier approach is to fetch all of them and search for related entries</p>

<pre class="lang-PHP prettyprint-override"><code>'elementType' =&gt; Category::class,
'criteria'    =&gt; [
    'group'   =&gt; 'yourGroup', 
],
'paginate'    =&gt; false,
'transformer' =&gt; function(Category $category) {
     $entries = Entry::find()
        -&gt;relatedTo($category)
        -&gt;select(['title'])
        -&gt;asArray()
        -&gt;column();

    return [
        'title'   =&gt; $category-&gt;title,
        'entries' =&gt; $entries     
    ];
}
</code></pre>

<p>Learn more about <a href="https://docs.craftcms.com/v3/dev/element-queries/" rel="nofollow noreferrer">Element Queries</a> especially the different ways to fetch entries via <a href="https://docs.craftcms.com/v3/dev/element-queries/#executing-element-queries" rel="nofollow noreferrer">one(), all(), column()</a></p>

CC BY-SA 4.0
2018-10-10T09:11:44.163Z
2018-10-10T09:11:44.163Z
Robin Schambach
<p>In Craft 3, I want to list all the categories in a group and all the entries related to them. How can I do this?</p>

<categories><plugin-elementapi>
Element API - Entries Related to a Category
# Element API - Entries Related to a Category

(Asked by: username_0 on 2018-10-10)

In Craft 3, I want to list all the categories in a group and all the entries related to them. How can I do this?

**Tags:** categories, plugin-elementapi


---

## 1 answers

### Answer by username_1 on 2018-10-10. Score: 2 | Accepted answer

The slower but easier approach is to fetch all of them and search for related entries
'elementType' => Category::class,
'criteria'    => [
    'group'   => 'yourGroup', 
],
'paginate'    => false,
'transformer' => function(Category $category) {
     $entries = Entry::find()
        ->relatedTo($category)
        ->select(['title'])
        ->asArray()
        ->column();

    return [
        'title'   => $category->title,
        'entries' => $entries     
    ];
}

Learn more about Element Queries especially the different ways to fetch entries via one(), all(), column()


---

