Answers.Body: <p>In order to get all related items from a <code>BaseRelationField</code> you'll need this syntax</p>
  
  <pre><code>$entry-&gt;getFieldValue('contentMatrix')-&gt;all() 
  </code></pre>
  
  <p>Thus your loop will be</p>
  
  <pre><code>foreach($entry-&gt;getFieldValue('contentMatrix')-&gt;all() as $block){
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-12T11:40:46.170Z
Answers.LastActivityDate: 2018-06-12T11:40:46.170Z
Answers.UserDisplayName: Robin Schambach
Body: <p>So I'm trying to get my Matrix as JSON with the <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">Craft CMS Element API</a> plugin.</p>
  
  <pre><code>&lt;?php
  
  use Craft\elements\Entry;
  
  return [
      'endpoints' =&gt; [
          'api/sites' =&gt; [
              'elementType' =&gt; craft\elements\Entry::class,
              'criteria' =&gt; ['section' =&gt; 'sites'],
              'elementsPerPage' =&gt; 1,
              'transformer' =&gt; function(Entry $entry) {
                  $matrixBlocks =[];
                  foreach ($entry-&gt;contentMatrix as $block) {
                      switch ($block-&gt;type-&gt;handle) {
                          case 'sectionTitleBlock':
                              $matrixBlocks[] = [
                                  'content' =&gt; $block-&gt;sectionTitleBlock
                              ];
                              break;
                      }
                  }
                  return [
                      'matrix' =&gt; $matrixBlocks,
                  ];
              },
              'paginate' =&gt; false,
              'pretty' =&gt; true,
          ],
      ]
  ];
  </code></pre>
  
  <p>The error message im getting is </p>
  
  <blockquote>
    <p>"error": {
        "code": 2,
        "message": "Invalid argument supplied for foreach()"
      }</p>
  </blockquote>
  
  <p>I think the problem is that <code>$entry-&gt;contentMatrix</code> is giving me a MatrixBlockQuery Element.</p>
  
  <p><strong>$sectionTitleBlock is a normal text field</strong></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-11T12:41:37.190Z
Comments.Text: Could you please try the correct syntax `foreach($entry->getFieldValue('contentMatrix')->all() as $block)`? Please make sure all entry types have a field in the layout `contentMatrix`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-12T11:30:34.477Z
Comments.Text: Yes. That fixed my problem! Thank you.
  
  May i ask were you got this correct syntax from? I can't find it on the plugins github page.
Comments.UserDisplayName: Leonardo
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-12T11:40:00.193Z
Comments.Text: It has nothing to do with the plugin, it's just the correct syntax but most people dislike it or don't use that. You should take a look at the source code, you'll see it everywhere
Comments.UserDisplayName: Robin Schambach
Tags: <craft3><plugin-elementapi>
Title: Element API: Get Matrix Content
ThreadText: # Element API: Get Matrix Content
  
  (Asked by: username_0 on 2018-06-11)
  
  So I'm trying to get my Matrix as JSON with the [Craft CMS Element API](https://github.com/craftcms/element-api) plugin.
  
  ```
  <?php
  
  use Craft\elements\Entry;
  
  return [
      'endpoints' => [
          'api/sites' => [
              'elementType' => craft\elements\Entry::class,
              'criteria' => ['section' => 'sites'],
              'elementsPerPage' => 1,
              'transformer' => function(Entry $entry) {
                  $matrixBlocks =[];
                  foreach ($entry->contentMatrix as $block) {
                      switch ($block->type->handle) {
                          case 'sectionTitleBlock':
                              $matrixBlocks[] = [
                                  'content' => $block->sectionTitleBlock
                              ];
                              break;
                      }
                  }
                  return [
                      'matrix' => $matrixBlocks,
                  ];
              },
              'paginate' => false,
              'pretty' => true,
          ],
      ]
  ];
  
  ```
  
  The error message im getting is
  
  > "error": {
  > "code": 2,
  > "message": "Invalid argument supplied for foreach()"
  > }
  
  I think the problem is that `$entry->contentMatrix` is giving me a MatrixBlockQuery Element.
  
  **$sectionTitleBlock is a normal text field**
  
  **Tags:** craft3, plugin-elementapi
  
  > **Comments:**
  > **username_1**: Could you please try the correct syntax `foreach($entry->getFieldValue('contentMatrix')->all() as $block)`? Please make sure all entry types have a field in the layout `contentMatrix`  
  > **username_0**: Yes. That fixed my problem! Thank you.  May i ask were you got this correct syntax from? I can't find it on the plugins github page.  
  > **username_1**: It has nothing to do with the plugin, it's just the correct syntax but most people dislike it or don't use that. You should take a look at the source code, you'll see it everywhere  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-06-12. Score: 2 | Accepted answer
  
  In order to get all related items from a `BaseRelationField` you'll need this syntax
  
  ```
  $entry->getFieldValue('contentMatrix')->all() 
  
  ```
  
  Thus your loop will be
  
  ```
  foreach($entry->getFieldValue('contentMatrix')->all() as $block){
  
  ```
  
  
  ---
  
  