How do you use the Element API plugin to output Assets from Matrix block for an entry? I have Single Section(entry) with Matrix field which contains Assets in blocks. I'm trying to fetch assets from a single Matrix block.

My element-api.php code is:

&lt;?php

use craft\elements\Entry;
use craft\elements\MatrixBlock;
use craft\elements\Asset;
use craft\helpers\UrlHelper;

return [
  'endpoints' =&gt; [
    'api/portfolio/&lt;blockId:\d+&gt;.json' =&gt; function($blockId) {
      return [
        'elementType' =&gt; MatrixBlock::class,
        'criteria' =&gt; ['ownerId' =&gt; '4351', 'fieldId' =&gt; '218', 'id' =&gt; $blockId],
        'pretty' =&gt; true,
      ];
    },
  ]
];


Part of output is:

   "join": [
            [
                "INNER JOIN",
                "{{%relations}} relations",
                [
                    "and",
                    "[[relations.targetId]] = [[elements.id]]",
                    {
                        "relations.sourceId": "4402",
                        "relations.fieldId": "219"
                    },
                    [
                        "or",
                        {
                            "relations.sourceSiteId": null
                        },
                        {
                            "relations.sourceSiteId": "1"
                        }
                    ]
                ]
            ]
        ],


so it looks like I'm trying to access related field id 219 which is Assets field inside Matrix block.

Fields structure:


Any ideas?
You need a transformer

'transformer' =&gt; function(MatrixBlock $element){
    return [
       'image' =&gt; $element-&gt;getFieldValue('assetFieldHandle')-&gt;one()-&gt;getUrl() 
    ];
}