Element API return a type of Category I'm just wondering when it comes to Element API is there a way of returning a type of Category.

I'm returning all the other information fine.



'fees.json' =&gt; [
    'elementType' =&gt; 'Entry',
        'criteria' =&gt; [
            'title' =&gt; craft()-&gt;request-&gt;getQuery('title'),
            'section' =&gt; 'fees',
        ],
        'transformer' =&gt; function(EntryModel $entry) {
            foreach ($entry-&gt;fee as $block) {
                $matrixSections[] = [
                    'first' =&gt; $block-&gt;firstInstallment,
                    'second' =&gt; $block-&gt;secondInstallment,
                    'full' =&gt; $block-&gt;full,
                    'grant' =&gt; $block-&gt;feeCategory
                ];
            }
        return [
            'title' =&gt; $entry-&gt;title,
            'menu' =&gt; [
                'sections' =&gt; $matrixSections
            ]
        ];
    }
]
The Element API plugin returns JSON, so you'll need to be specific about what you want to return for your categories – i.e. you can't return a CategoryModel (or an array of CategoryModels) in full directly from a Categories field.

The easiest option is to use array_map() like this:

'first' =&gt; $block-&gt;firstInstallment,
'second' =&gt; $block-&gt;secondInstallment,
'full' =&gt; $block-&gt;full,
'grant' =&gt; array_map(function (CategoryModel $category) {
    return [
        'id' =&gt; $category-&gt;id,
        'title' =&gt; $category-&gt;title,
    ];
}, $block-&gt;categoriesField-&gt;find()),


Of course, if your individual category has a lot of fields, you can always create an external function in your elementapi.php file and use that for the array_map call, instead of inlining the function (the external function needs to live above the return ['endpoints']... part):

function getCategoryData(CategoryModel $category)
{
    return [
        'id' =&gt; $category-&gt;id,
        'title' =&gt; $category-&gt;title,
        ...
    ];
}

...

'grant' =&gt; array_map('getCategoryData', $block-&gt;categoriesField-&gt;find()),


Also, there's the $category-&gt;getAttributes() method which can be handy if you want to return all stock attributes (i.e. id, dateUpdated etc – pretty much everything sans the title, for some reason):

function getCategoryData(CategoryModel $category)
{
    return array_merge($category-&gt;getAttributes(), [
        'title' =&gt; $category-&gt;title,
        'someCustomField' =&gt; $category-&gt;someCustomField,
    ];
}