Answers.Body: <p>Solved it like this:</p>
  
  <pre><code>//categories
      'api/v1/ship/category/&lt;catId:\d+&gt;.json' =&gt; function($catId) {
          return [
              'elementType' =&gt; Category::class,
              'criteria'    =&gt; ['id'   =&gt; $catId],
              'paginate'    =&gt; false,
              'transformer' =&gt; function(Category $category) {
                  $entries = Entry::find()-&gt;relatedTo($category)-&gt;all();
  
                  $en=[];
                  foreach($entries as $entry) {
  
  
                      //Final return
                      $en[]= [
                          "type" =&gt; "shipItem",
                          'sendtoseafarerhelp' =&gt; $entry-&gt;sendToSeafarerHelp,
                          'title' =&gt; $entry-&gt;title,
                          'url' =&gt; $entry-&gt;url,
                          'categoryTitle' =&gt; $entry-&gt;shipCategory[0]-&gt;title,
                          'categorySlug' =&gt; $entry-&gt;shipCategory[0]-&gt;slug,
                          'categoryUrl' =&gt; $entry-&gt;shipCategory[0]-&gt;uri,
                          'summary' =&gt; $entry-&gt;shipIntroText,
                          'descripton' =&gt; $entry-&gt;shipDescription,
                          'pdfs' =&gt; $pdfs,
                          'thumbs' =&gt; $thumbs,
                          'sponsors' =&gt; $sponsors                            
                      ];
                  }
  
                  return [
                      'title'   =&gt; $category-&gt;title,
                      'entries' =&gt; $en
                  ];
              }
          ];
      },
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-12T04:14:49.557Z
Answers.Text: joomkit: $entries = Entry::find()->relatedTo($category)->all(); Doesn't return any entries though I have two entries on the same Category. Is there any specific way a Category should be assigned to an Entry?
Answers.UserDisplayName: Wasantha Wijayaratna
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-17T12:41:26.573Z
Answers.LastActivityDate: 2018-11-17T12:41:26.573Z
Answers.UserDisplayName: joomkit
Body: <p>I am having trouble getting the related category passed thru to the entry api endpoint.</p>
  
  <p>I cant use :</p>
  
  <pre><code>   $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
          $criteria-&gt;id = $catId;
          $category = $criteria-&gt;first();
  </code></pre>
  
  <p>What is the equivalent in craft 3 api v2?</p>
  
  <pre><code>'api/v1/ship/category/&lt;catId:\d+&gt;.json' =&gt; function($catId) {
  //old api craft 2
          $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
          $criteria-&gt;id = $catId;
          $category = $criteria-&gt;first();
  
  //api craft 3
          return [
              'elementType' =&gt; Entry::class,
              'elementsPerPage' =&gt; 30,
              'criteria' =&gt; [
                  'criteria' =&gt; ['section' =&gt; 'ship'],
                  'relatedTo' =&gt; ['targetElement' =&gt; $category],
              ],
              '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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-17T01:17:40.973Z
Comments.Text: Would you mind adding your solution as an official answer @joomkit?
Comments.UserDisplayName: Brad Bell
Tags: <plugin-elementapi>
Title: Element-API migration - entries by category
ThreadText: # Element-API migration - entries by category
  
  (Asked by: username_0 on 2018-11-15)
  
  I am having trouble getting the related category passed thru to the entry api endpoint.
  
  I cant use :
  
  ```
     $criteria = craft()->elements->getCriteria(ElementType::Category);
          $criteria->id = $catId;
          $category = $criteria->first();
  
  ```
  
  What is the equivalent in craft 3 api v2?
  
  ```
  'api/v1/ship/category/<catId:\d+>.json' => function($catId) {
  //old api craft 2
          $criteria = craft()->elements->getCriteria(ElementType::Category);
          $criteria->id = $catId;
          $category = $criteria->first();
  
  //api craft 3
          return [
              'elementType' => Entry::class,
              'elementsPerPage' => 30,
              'criteria' => [
                  'criteria' => ['section' => 'ship'],
                  'relatedTo' => ['targetElement' => $category],
              ],
              'transformer' => function(Entry $entry) {
                  return [
                      'title' => $entry->title,
                      'id' => (int) $entry->id,
                      'slug' => $entry->slug,
                      'postDate' => $entry->postDate
                  ];
              },
              'pretty' => true
          ];
      },
  
  ```
  
  **Tags:** plugin-elementapi
  
  > **Comments:**
  > **username_1**: Would you mind adding your solution as an official answer @username_0?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-11-17. Score: 1 | Accepted answer
  
  Solved it like this:
  
  ```
  //categories
      'api/v1/ship/category/<catId:\d+>.json' => function($catId) {
          return [
              'elementType' => Category::class,
              'criteria'    => ['id'   => $catId],
              'paginate'    => false,
              'transformer' => function(Category $category) {
                  $entries = Entry::find()->relatedTo($category)->all();
  
                  $en=[];
                  foreach($entries as $entry) {
  
                      //Final return
                      $en[]= [
                          "type" => "shipItem",
                          'sendtoseafarerhelp' => $entry->sendToSeafarerHelp,
                          'title' => $entry->title,
                          'url' => $entry->url,
                          'categoryTitle' => $entry->shipCategory[0]->title,
                          'categorySlug' => $entry->shipCategory[0]->slug,
                          'categoryUrl' => $entry->shipCategory[0]->uri,
                          'summary' => $entry->shipIntroText,
                          'descripton' => $entry->shipDescription,
                          'pdfs' => $pdfs,
                          'thumbs' => $thumbs,
                          'sponsors' => $sponsors                            
                      ];
                  }
  
                  return [
                      'title'   => $category->title,
                      'entries' => $en
                  ];
              }
          ];
      },
  
  ```
  
  > **Comments:**
  > **username_2**: username_0: $entries = Entry::find()->relatedTo($category)->all(); Doesn't return any entries though I have two entries on the same Category. Is there any specific way a Category should be assigned to an Entry?  
  
  ---
  
  