Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-10T09:11:44.163Z
Answers.LastActivityDate: 2018-10-10T09:11:44.163Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Tags: <categories><plugin-elementapi>
Title: Element API - Entries Related to a Category
ThreadText: # 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
  
  ```PHP
  '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](https://docs.craftcms.com/v3/dev/element-queries/) especially the different ways to fetch entries via [one(), all(), column()](https://docs.craftcms.com/v3/dev/element-queries/#executing-element-queries)
  
  
  ---
  
  