Answers.Body: <p>Your <code>targetElement</code> needs to be the category object or the ID of your category.
  So if the id of your category is 5 you need</p>
  
  <pre><code>'relatedTo' =&gt; ['targetElement' =&gt; 5],
  </code></pre>
  
  <p>If you really want to be able to fetch your category by slug (which I would not suggest since your slug can be changed in the cp but the ID can't, if someone changes the slug by accident your entire logic will break) you can create an <code>ElementCriteriaModel</code> to search for the category with the slug and insert the object there.</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
  $criteria-&gt;slug = $slug; 
  $category = $criteria-&gt;find();
  </code></pre>
  
  <p>And inside your array</p>
  
  <pre><code>'relatedTo' =&gt; ['targetElement' =&gt; $category],
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-03T16:00:53.153Z
Answers.Text: Targeting by ID definitely seems to make more sense now that you mention that, so that 'relatedTo' => ['targetElement' => 5], how does that know that it is a category its going to be relating to? actually, that ID is going to be unique - silly me
Answers.UserDisplayName: jamie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-03T16:05:28.667Z
Answers.Text: Works perfectly nice one Robin
Answers.UserDisplayName: jamie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-03T16:38:43.847Z
Answers.Text: Just to explain that: In Craft, there are elements (assets, users, categories, entries) every time you insert one of those 4 types a record in `craft_elements` is generated with some information about the record. When you insert an ID in the `relatedTo` it checks the record from `craft_elements` - in your case it will find a `category` in the `type` column - so it knows its a category and not an entry. That's the reason why you can relate everything with everything because there is one really huge table for all records
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-03T15:51:36.557Z
Answers.LastActivityDate: 2018-02-03T15:58:45.400Z
Answers.LastEditDate: 2018-02-03T15:58:45.400Z
Answers.UserDisplayName: Robin Schambach
Body: <p>With v2 of the Element API plugin, I'm having issues with working out how to return entries that are in a particular category.</p>
  
  <p>I've got the slug in the API route but can't work out what the query should be in the transformer.</p>
  
  <p>Should I be doing this query in a total different way?  I keep thinking I've just gone down the wrong way to do it completely.</p>
  
  <pre><code>'api/v1/category/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {
  
          return [
              'elementType' =&gt; Entry::class,
              'elementsPerPage' =&gt; 30,
              'criteria' =&gt; [
                'section' =&gt; 'posts',
                'relatedTo' =&gt; ['targetElement' =&gt; ***category slug($slug)***],
              ],
              'transformer' =&gt; function(Entry $entry) {
                  return [
                      'title' =&gt; $entry-&gt;title,
                      'id' =&gt; (int) $entry-&gt;id,
                      'slug' =&gt; $entry-&gt;slug,
                      'postDate' =&gt; $entry-&gt;postDate
                  ];
              },
              'pretty' =&gt; true
          ];
      }
  </code></pre>
  
Tags: <entries><categories><craft3><plugin-elementapi>
Title: Element API display entries by category
ThreadText: # Element API display entries by category
  
  (Asked by: username_0 on 2018-02-03)
  
  With v2 of the Element API plugin, I'm having issues with working out how to return entries that are in a particular category.
  
  I've got the slug in the API route but can't work out what the query should be in the transformer.
  
  Should I be doing this query in a total different way? I keep thinking I've just gone down the wrong way to do it completely.
  
  ```
  'api/v1/category/<slug:{slug}>.json' => function($slug) {
  
          return [
              'elementType' => Entry::class,
              'elementsPerPage' => 30,
              'criteria' => [
                'section' => 'posts',
                'relatedTo' => ['targetElement' => ***category slug($slug)***],
              ],
              'transformer' => function(Entry $entry) {
                  return [
                      'title' => $entry->title,
                      'id' => (int) $entry->id,
                      'slug' => $entry->slug,
                      'postDate' => $entry->postDate
                  ];
              },
              'pretty' => true
          ];
      }
  
  ```
  
  **Tags:** entries, categories, craft3, plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-03. Score: 2 | Accepted answer
  
  Your `targetElement` needs to be the category object or the ID of your category.
  So if the id of your category is 5 you need
  
  ```
  'relatedTo' => ['targetElement' => 5],
  
  ```
  
  If you really want to be able to fetch your category by slug (which I would not suggest since your slug can be changed in the cp but the ID can't, if someone changes the slug by accident your entire logic will break) you can create an `ElementCriteriaModel` to search for the category with the slug and insert the object there.
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Category);
  $criteria->slug = $slug; 
  $category = $criteria->find();
  
  ```
  
  And inside your array
  
  ```
  'relatedTo' => ['targetElement' => $category],
  
  ```
  
  > **Comments:**
  > **username_0**: Targeting by ID definitely seems to make more sense now that you mention that, so that 'relatedTo' => ['targetElement' => 5], how does that know that it is a category its going to be relating to? actually, that ID is going to be unique - silly me  
  > **username_0**: Works perfectly nice one username_1  
  > **username_1**: Just to explain that: In Craft, there are elements (assets, users, categories, entries) every time you insert one of those 4 types a record in `craft_elements` is generated with some information about the record. When you insert an ID in the `relatedTo` it checks the record from `craft_elements` - in your case it will find a `category` in the `type` column - so it knows its a category and not an entry. That's the reason why you can relate everything with everything because there is one really huge table for all records  
  
  ---
  
  