Answers.Body: <p>You’d do this with a brand new element query using the <a href="https://craftcms.com/docs/templating/craft.entries#relatedTo" rel="noreferrer">relatedTo</a> param.</p>
  
  
  
  <p>Craft 2:</p>
  
  <pre class="lang-php prettyprint-override"><code>// This is the PHP equivalent of typing `craft.entries()` in a template:
  $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  
  // Only fetch Location entries that are related to the Site entry:
  $criteria-&gt;section = 'locations';
  $criteria-&gt;relatedTo = ['targetElement' =&gt; $entry];
  
  $locations = $criteria-&gt;find();
  </code></pre>
  
  <p>Craft 3:</p>
  
  <pre class="lang-php prettyprint-override"><code>// This is the PHP equivalent of typing `craft.entries()` in a template:
  $query = \craft\elements\Entry::find();
  
  // Only fetch Location entries that are related to the Site entry:
  $query-&gt;section('locations');
  $query-&gt;relatedTo(['targetElement' =&gt; $entry]);
  
  $locations = $query-&gt;all();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-01T22:33:05.363Z
Answers.Text: Looks like this broke in the updated version we're using for Craft 3. Getting a _Call to undefined function craft()_ error when loading up the endpoint.
Answers.UserDisplayName: Roger Erik Tinch
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-02T14:09:41.577Z
Answers.Text: @Roger you might want to read the documentation  https://github.com/craftcms/docs/blob/master/en/element-queries.md you'll find many things you need to know for Craft 3 but if you are going to create a more complex site (with custom plugins) you must go through the source code or analyze other plugins in order to use everything properly
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-31T19:32:31.667Z
Answers.LastActivityDate: 2017-12-02T13:55:51.603Z
Answers.LastEditDate: 2017-12-02T13:55:51.603Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I have two sections, each with a single entry type:</p>
  
  <ul>
  <li>Sites</li>
  <li>Locations</li>
  </ul>
  
  <p>In the Locations entry type I have a Entries field called Site that you can use to relate a Location to a Site entry.</p>
  
  <p>My questions is: how do I show which locations were related to a Site entry when using the Element API? I'd like to display it as an array when using a sites/.json endpoint.</p>
  
  <p>This is what I've tried, but it breaks the endpoint:</p>
  
  <pre><code>'v1/sites/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
    return [
      'elementType' =&gt; 'Entry',
      'criteria' =&gt; ['id' =&gt; $entryId],
      'first' =&gt; true,
      'transformer' =&gt; function(EntryModel $entry) {
        $locations = $entry-&gt;getChildren()-&gt;type('locations')-&gt;find();
        foreach ($locations as $location) {
          $locationsInfo[] = [
            'name' =&gt; $location-&gt;title
          ];
        }
  
        return [
          'id' =&gt; $entry-&gt;id,
          'name' =&gt; $entry-&gt;title,
          'tagline' =&gt; $entry-&gt;tagline,
          'about' =&gt; $entry-&gt;about-&gt;getParsedContent(),
          'locations' =&gt; $locationsInfo,
          'jsonUrl' =&gt; UrlHelper::getUrl("v1/sites/{$entry-&gt;id}.json")
        ];
      },
    ];
  },
  </code></pre>
  
Tags: <plugin-elementapi>
Title: Calling Related Entries using the Element API
ThreadText: # Calling Related Entries using the Element API
  
  (Asked by: username_0 on 2016-03-31)
  
  I have two sections, each with a single entry type:
  
  * Sites
  * Locations
  
  In the Locations entry type I have a Entries field called Site that you can use to relate a Location to a Site entry.
  
  My questions is: how do I show which locations were related to a Site entry when using the Element API? I'd like to display it as an array when using a sites/.json endpoint.
  
  This is what I've tried, but it breaks the endpoint:
  
  ```
  'v1/sites/<entryId:\d+>.json' => function($entryId) {
    return [
      'elementType' => 'Entry',
      'criteria' => ['id' => $entryId],
      'first' => true,
      'transformer' => function(EntryModel $entry) {
        $locations = $entry->getChildren()->type('locations')->find();
        foreach ($locations as $location) {
          $locationsInfo[] = [
            'name' => $location->title
          ];
        }
  
        return [
          'id' => $entry->id,
          'name' => $entry->title,
          'tagline' => $entry->tagline,
          'about' => $entry->about->getParsedContent(),
          'locations' => $locationsInfo,
          'jsonUrl' => UrlHelper::getUrl("v1/sites/{$entry->id}.json")
        ];
      },
    ];
  },
  
  ```
  
  **Tags:** plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-31. Score: 9 | Accepted answer
  
  You’d do this with a brand new element query using the [relatedTo](https://craftcms.com/docs/templating/craft.entries#relatedTo) param.
  
  Craft 2:
  
  ```php
  // This is the PHP equivalent of typing `craft.entries()` in a template:
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  
  // Only fetch Location entries that are related to the Site entry:
  $criteria->section = 'locations';
  $criteria->relatedTo = ['targetElement' => $entry];
  
  $locations = $criteria->find();
  
  ```
  
  Craft 3:
  
  ```php
  // This is the PHP equivalent of typing `craft.entries()` in a template:
  $query = \craft\elements\Entry::find();
  
  // Only fetch Location entries that are related to the Site entry:
  $query->section('locations');
  $query->relatedTo(['targetElement' => $entry]);
  
  $locations = $query->all();
  
  ```
  
  > **Comments:**
  > **username_0**: Looks like this broke in the updated version we're using for Craft 3. Getting a _Call to undefined function craft()_ error when loading up the endpoint.  
  > **username_2**: @username_0 you might want to read the documentation  https://github.com/craftcms/docs/blob/master/en/element-queries.md you'll find many things you need to know for Craft 3 but if you are going to create a more complex site (with custom plugins) you must go through the source code or analyze other plugins in order to use everything properly  
  
  ---
  
  