Answers.Body: <p>A couple of issues:</p>
  
  <p>First, the <a href="https://craftcms.com/classreference/models/BaseElementModel#getParent-detail" rel="noreferrer"><code>EntryModel::getParent()</code></a> method returns an <a href="https://craftcms.com/classreference/models/EntryModel" rel="noreferrer">EntryModel</a>, not an <a href="https://craftcms.com/classreference/models/ElementCriteriaModel" rel="noreferrer">ElementCriteriaModel</a>, which means that calling <code>getParent()-&gt;first()</code> will throw an exception. In other words, you'll need to remove the call to <code>first()</code>.</p>
  
  <p>Second, you'll need to be specific about which fields you want to return from your parent entry – just passing in the raw EntryModel won't work – so your code should look something like this:</p>
  
  <pre><code>'transformer' =&gt; function(EntryModel $entry) {
      $parent = $entry-&gt;getParent();
      return [
          'title' =&gt; $entry-&gt;title,
          'url' =&gt; $entry-&gt;url,
          'level' =&gt; $entry-&gt;level,
          'parent' =&gt; $parent ? [
              'title' =&gt; $parent-&gt;title,
              'url' =&gt; $parent-&gt;url,
              'someCustomField' =&gt; $parent-&gt;someCustomField,
          ] : null,
      ];
    },
  </code></pre>
  
  <p>In the above, I'm using a ternary operator to conditionally include the array containing the parent entry's data (the condition being if the parent entry exists or not). If you want your code to be less "inline", you could also do something like this:</p>
  
  <pre><code>'transformer' =&gt; function (EntryModel $entry) {
      $parent = $entry-&gt;getParent();
      $return = [
          'title' =&gt; $entry-&gt;title,
          'url' =&gt; $entry-&gt;url,
          'level' =&gt; $entry-&gt;level
      ];
      if ($parent) {
          $return['parent'] = [
              'title' =&gt; $parent-&gt;title,
              'url' =&gt; $parent-&gt;url,
              'someCustomField' =&gt; $parent-&gt;someCustomField,
          ];
      }
      return $return;
  },
  </code></pre>
  
  <p>Finally, if you need to be specific about the parent entry's level, you'll need to swap out the <code>getParent()</code> method for <a href="https://craftcms.com/classreference/models/BaseElementModel#getAncestors-detail" rel="noreferrer"><code>EntryMode::getAncestors()</code></a> (which <em>does</em> return an ElementCriteriaModel). Here's how you'd get the top level parent:</p>
  
  <pre><code>$parent = $entry-&gt;getAncestors()-&gt;first();
  </code></pre>
  
  <p>...and since <code>getAncestors()</code> returns an ECM, pulling a parent entry with a specific level is trivial:</p>
  
  <pre><code>$parent = $entry-&gt;getAncestors()-&gt;level(2)-&gt;first();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-05T21:53:18.080Z
Answers.Text: Hi Mats, thank you for your detailed and extremely helpful explanation. Could I ask one more question if you don't mind? How come `getParent() ` returns a EntryModel where as `getAncestors()` returns an ECM. What determines this and is there a way to see which methods return which models or does that just come with experience?
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-06T10:56:03.957Z
Answers.Text: Hi Gareth, very cool I was able to help you out. You'll need to check the official [class reference](https://craftcms.com/classreference) to find out what a certain method will return (and which arguments it accepts). In your case, you'd have to find the reference for the [EntryModel class](https://craftcms.com/classreference/models/EntryModel) which will have info on the `getParent()` and `getAncestors()` mehods. Beyond that, Craft is pretty consistent semantically – from their names, it's safe to assume that `getParent()` should return a single element and `getAncestors()` multiple elements.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-05T16:59:34.803Z
Answers.LastActivityDate: 2016-11-05T16:59:34.803Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>How can you access the parent of a section using the Element API? I have the following where I use the <code>getParent()</code> function which does return an empty object when an entry has a parent.</p>
  
  <p>Do I need to pass in the element criteria model or something?</p>
  
  <p>Also could someone expand on this to show how you just get the level 1 parent or the level 2 parent please.</p>
  
  <pre><code>return [
    'endpoints' =&gt; [
      'brands.json' =&gt; [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; ['section' =&gt; 'brand'],
        'transformer' =&gt; function(EntryModel $entry) {
          return [
              'title' =&gt; $entry-&gt;title,
              'url' =&gt; $entry-&gt;url,
              'level' =&gt; $entry-&gt;level,
              'parent' =&gt; $entry-&gt;getParent()-&gt;first(),
          ];
        },
      ],
    ]
  ];
  </code></pre>
  
Tags: <plugins><plugin-elementapi>
Title: Get parent entry from a section using the Element API
ThreadText: # Get parent entry from a section using the Element API
  
  (Asked by: username_0 on 2016-11-05)
  
  How can you access the parent of a section using the Element API? I have the following where I use the `getParent()` function which does return an empty object when an entry has a parent.
  
  Do I need to pass in the element criteria model or something?
  
  Also could someone expand on this to show how you just get the level 1 parent or the level 2 parent please.
  
  ```
  return [
    'endpoints' => [
      'brands.json' => [
        'elementType' => 'Entry',
        'criteria' => ['section' => 'brand'],
        'transformer' => function(EntryModel $entry) {
          return [
              'title' => $entry->title,
              'url' => $entry->url,
              'level' => $entry->level,
              'parent' => $entry->getParent()->first(),
          ];
        },
      ],
    ]
  ];
  
  ```
  
  **Tags:** plugins, plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-11-05. Score: 5 | Accepted answer
  
  A couple of issues:
  
  First, the [`EntryModel::getParent()`](https://craftcms.com/classreference/models/BaseElementModel#getParent-detail) method returns an [EntryModel](https://craftcms.com/classreference/models/EntryModel), not an [ElementCriteriaModel](https://craftcms.com/classreference/models/ElementCriteriaModel), which means that calling `getParent()->first()` will throw an exception. In other words, you'll need to remove the call to `first()`.
  
  Second, you'll need to be specific about which fields you want to return from your parent entry – just passing in the raw EntryModel won't work – so your code should look something like this:
  
  ```
  'transformer' => function(EntryModel $entry) {
      $parent = $entry->getParent();
      return [
          'title' => $entry->title,
          'url' => $entry->url,
          'level' => $entry->level,
          'parent' => $parent ? [
              'title' => $parent->title,
              'url' => $parent->url,
              'someCustomField' => $parent->someCustomField,
          ] : null,
      ];
    },
  
  ```
  
  In the above, I'm using a ternary operator to conditionally include the array containing the parent entry's data (the condition being if the parent entry exists or not). If you want your code to be less "inline", you could also do something like this:
  
  ```
  'transformer' => function (EntryModel $entry) {
      $parent = $entry->getParent();
      $return = [
          'title' => $entry->title,
          'url' => $entry->url,
          'level' => $entry->level
      ];
      if ($parent) {
          $return['parent'] = [
              'title' => $parent->title,
              'url' => $parent->url,
              'someCustomField' => $parent->someCustomField,
          ];
      }
      return $return;
  },
  
  ```
  
  Finally, if you need to be specific about the parent entry's level, you'll need to swap out the `getParent()` method for [`EntryMode::getAncestors()`](https://craftcms.com/classreference/models/BaseElementModel#getAncestors-detail) (which *does* return an ElementCriteriaModel). Here's how you'd get the top level parent:
  
  ```
  $parent = $entry->getAncestors()->first();
  
  ```
  
  ...and since `getAncestors()` returns an ECM, pulling a parent entry with a specific level is trivial:
  
  ```
  $parent = $entry->getAncestors()->level(2)->first();
  
  ```
  
  > **Comments:**
  > **username_0**: Hi username_1, thank you for your detailed and extremely helpful explanation. Could I ask one more question if you don't mind? How come `getParent() ` returns a EntryModel where as `getAncestors()` returns an ECM. What determines this and is there a way to see which methods return which models or does that just come with experience?  
  > **username_1**: Hi username_0, very cool I was able to help you out. You'll need to check the official [class reference](https://craftcms.com/classreference) to find out what a certain method will return (and which arguments it accepts). In your case, you'd have to find the reference for the [EntryModel class](https://craftcms.com/classreference/models/EntryModel) which will have info on the `getParent()` and `getAncestors()` mehods. Beyond that, Craft is pretty consistent semantically – from their names, it's safe to assume that `getParent()` should return a single element and `getAncestors()` multiple elements.  
  
  ---
  
  