I have an API endpoint set up for a category group. This currently provides some details of all entries that have been assigned that category (i.e. related entries). That works well for some high level details, but I now need to add support for a Matrix field that those related entries contain. Here's the current setup:
'series.json' => function() {
return [
'elementType' => Category::class,
'criteria' => ['group' => 'series'],
'paginate' => false,
'transformer' => function(Category $entry) {
$entries = Entry::find()
->relatedTo($entry)
->select(['slug','title','field_rating AS rating','field_pageCopy AS desc','postDate AS date'])
->asArray()
->all();
$typeArray = array_map('strval', $entry->mediaType->all());
return [
'id' => $entry->id,
'slug' => $entry->slug,
'title' => $entry->title,
'type' => strtolower($typeArray[0]),
'desc' => $entry->blurb,
'entries' => $entries
];
}
];
}
However, if I try to add the Matrix field to the select()
argument it doesn't work. If I remove select()
I can see why: it isn't a listed field. If I remove asArray()
then I can see all the relevant fields, but I can't limit the ones available, which is undesirable as it exposes a lot of excess information.
I feel like their might be a better way to format my transformer to begin with, but I'm at a bit of a loss when it comes to the docs.