Answers.Body: <p>You need a <a href="https://github.com/craftcms/element-api/blob/v2/README.md#transformer" rel="nofollow noreferrer">transformer</a></p>
  
  <pre><code>'transformer' =&gt; function(MatrixBlock $element){
      return [
         'image' =&gt; $element-&gt;getFieldValue('assetFieldHandle')-&gt;one()-&gt;getUrl() 
      ];
  } 
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-09T05:13:40.200Z
Answers.LastActivityDate: 2018-10-09T05:13:40.200Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have Single Section(entry) with Matrix field which contains Assets in blocks. I'm trying to fetch assets from a single Matrix block.</p>
  
  <p>My <code>element-api.php</code> code is:</p>
  
  <pre><code>&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,
        ];
      },
    ]
  ];
  </code></pre>
  
  <p>Part of output is:</p>
  
  <pre><code>   "join": [
              [
                  "INNER JOIN",
                  "{{%relations}} relations",
                  [
                      "and",
                      "[[relations.targetId]] = [[elements.id]]",
                      {
                          "relations.sourceId": "4402",
                          "relations.fieldId": "219"
                      },
                      [
                          "or",
                          {
                              "relations.sourceSiteId": null
                          },
                          {
                              "relations.sourceSiteId": "1"
                          }
                      ]
                  ]
              ]
          ],
  </code></pre>
  
  <p>so it looks like I'm trying to access related field id 219 which is Assets field inside Matrix block.</p>
  
  <p>Fields structure:
  <a href="https://i.sstatic.net/Ccw8D.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ccw8D.png" alt="enter image description here"></a></p>
  
  <p>Any ideas?</p>
  
Tags: <plugin-elementapi>
Title: How do you use the Element API plugin to output Assets from Matrix block for an entry?
ThreadText: # How do you use the Element API plugin to output Assets from Matrix block for an entry?
  
  (Asked by: username_0 on 2018-10-09)
  
  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:
  
  ```
  <?php
  
  use craft\elements\Entry;
  use craft\elements\MatrixBlock;
  use craft\elements\Asset;
  use craft\helpers\UrlHelper;
  
  return [
    'endpoints' => [
      'api/portfolio/<blockId:\d+>.json' => function($blockId) {
        return [
          'elementType' => MatrixBlock::class,
          'criteria' => ['ownerId' => '4351', 'fieldId' => '218', 'id' => $blockId],
          'pretty' => 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:
  [![enter image description here](https://i.sstatic.net/Ccw8D.png)](https://i.sstatic.net/Ccw8D.png)
  
  Any ideas?
  
  **Tags:** plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-09. Score: 2 | Accepted answer
  
  You need a [transformer](https://github.com/craftcms/element-api/blob/v2/README.md#transformer)
  
  ```
  'transformer' => function(MatrixBlock $element){
      return [
         'image' => $element->getFieldValue('assetFieldHandle')->one()->getUrl() 
      ];
  } 
  
  ```
  
  
  ---
  
  