Get parent entry from a section using the Element API 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' =&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(),
        ];
      },
    ],
  ]
];
A couple of issues:

First, the EntryModel::getParent() method returns an EntryModel, not an ElementCriteriaModel, which means that calling getParent()-&gt;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' =&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,
    ];
  },


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' =&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;
},


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() (which does return an ElementCriteriaModel). Here's how you'd get the top level parent:

$parent = $entry-&gt;getAncestors()-&gt;first();


...and since getAncestors() returns an ECM, pulling a parent entry with a specific level is trivial:

$parent = $entry-&gt;getAncestors()-&gt;level(2)-&gt;first();