Answers.Body: <p>Figured it out in the end. As an array is being returned I just needed a for loop to get inside it.</p>
  
  <p>Solution...</p>
  
  <pre><code>                    if($entry-&gt;relatedContent){
                          $index = -1;
                          foreach ($entry-&gt;relatedContent-&gt;all() as $block) {
                              switch ($block-&gt;type-&gt;handle) {
                                  case 'relateContent':
                                      $index++;
                                      $relatedFeaturedEntries =  $block-&gt;featured-&gt;first();
                                      $relationsData[$index] = [
                                          'title' =&gt; $relatedFeaturedEntries-&gt;title,
                                          'url' =&gt; $relatedFeaturedEntries-&gt;url
                                      ];
  
                                      $relationsData[$index]['subItems'] = [];
                                      $relatedSubEntries =  $block-&gt;related-&gt;all();
                                      foreach ($relatedSubEntries as $relatedSubEntry) {
                                          $relationsData[$index]['subItems'][] = [
                                              'title' =&gt; $relatedSubEntry-&gt;title,
                                              'url' =&gt; $relatedSubEntry-&gt;url
                                          ];
                                      }
                                  break;
                              }
                          }
                      }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-24T09:21:34.827Z
Answers.LastActivityDate: 2019-05-24T09:21:34.827Z
Answers.UserDisplayName: Matt Simon
Body: <p>I'm getting matrix block content from an entry type which works fine for the most part. It's probably clearer to just paste in the code:</p>
  
  <p>Current element api code:</p>
  
  <pre><code>
  $relationsData = [];    
  if($entry-&gt;relatedContent){
      $index = -1;
      foreach ($entry-&gt;relatedContent-&gt;all() as $block) {
          switch ($block-&gt;type-&gt;handle) {
              case 'relateContent':
                  $index++;
                  $relatedFeaturedEntries =  $block-&gt;featured-&gt;first();
                  $relatedSubEntries =  $block-&gt;related-&gt;first();
                  $relationsData[$index] = [
                      'title' =&gt; $relatedFeaturedEntries-&gt;title,
                      'url' =&gt; $relatedFeaturedEntries-&gt;url
                  ];
                  $relationsData[$index]['subItems'] = [];
                  $relationsData[$index]['subItems'][] = [
                      'title' =&gt; $relatedSubEntries-&gt;title,
                      'url' =&gt; $relatedSubEntries-&gt;url
                  ];
              break;
          }
      }
  }
  </code></pre>
  
  <p>Which outputs:</p>
  
  <pre><code>      "relations": [
          {
            "title": "Testing workflow",
            "url": "http://localhost/blah",
            "subItems": [
              {
                "title": "Help &amp; Guidance",
                "url": "http://localhost/help-guidance"
              }
            ]
          },
          {
            "title": "Taking part in the blah",
            "url": "http://localhost/blah",
            "subItems": [
              {
                "title": "blah timeline",
                "url": "http://localhost/blah"
              }
            ]
          },
          {
            "title": "Testing workflow",
            "url": "http://localhost/blah",
            "subItems": [
              {
                "title": "Completing on paper",
                "url": "http://localhost/blah"
              }
            ]
          }
        ],
  </code></pre>
  
  <p>There should be more <code>subItems</code> in each <code>subItems</code> array but I am only getting the first because I'm fetching them using <code>$relatedSubEntries =  $block-&gt;related-&gt;first();</code> But nothing else works and just errors i.e. <code>all()</code> or removing <code>first()</code></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-22T18:10:55.223Z
Comments.Text: "But nothing else works and just errors" - what are the errors?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-23T08:14:11.730Z
Comments.Text: `Trying to get property of non-object`
Comments.UserDisplayName: Matt Simon
Tags: <matrix><plugin-elementapi>
Title: Element API unable to get all() of matrix field
ThreadText: # Element API unable to get all() of matrix field
  
  (Asked by: username_0 on 2019-05-22)
  
  I'm getting matrix block content from an entry type which works fine for the most part. It's probably clearer to just paste in the code:
  
  Current element api code:
  
  ```
  
  $relationsData = [];    
  if($entry->relatedContent){
      $index = -1;
      foreach ($entry->relatedContent->all() as $block) {
          switch ($block->type->handle) {
              case 'relateContent':
                  $index++;
                  $relatedFeaturedEntries =  $block->featured->first();
                  $relatedSubEntries =  $block->related->first();
                  $relationsData[$index] = [
                      'title' => $relatedFeaturedEntries->title,
                      'url' => $relatedFeaturedEntries->url
                  ];
                  $relationsData[$index]['subItems'] = [];
                  $relationsData[$index]['subItems'][] = [
                      'title' => $relatedSubEntries->title,
                      'url' => $relatedSubEntries->url
                  ];
              break;
          }
      }
  }
  
  ```
  
  Which outputs:
  
  ```
        "relations": [
          {
            "title": "Testing workflow",
            "url": "http://localhost/blah",
            "subItems": [
              {
                "title": "Help & Guidance",
                "url": "http://localhost/help-guidance"
              }
            ]
          },
          {
            "title": "Taking part in the blah",
            "url": "http://localhost/blah",
            "subItems": [
              {
                "title": "blah timeline",
                "url": "http://localhost/blah"
              }
            ]
          },
          {
            "title": "Testing workflow",
            "url": "http://localhost/blah",
            "subItems": [
              {
                "title": "Completing on paper",
                "url": "http://localhost/blah"
              }
            ]
          }
        ],
  
  ```
  
  There should be more `subItems` in each `subItems` array but I am only getting the first because I'm fetching them using `$relatedSubEntries = $block->related->first();` But nothing else works and just errors i.e. `all()` or removing `first()`
  
  **Tags:** matrix, plugin-elementapi
  
  > **Comments:**
  > **username_1**: "But nothing else works and just errors" - what are the errors?  
  > **username_0**: `Trying to get property of non-object`  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-05-24. Score: 0 | Accepted answer
  
  Figured it out in the end. As an array is being returned I just needed a for loop to get inside it.
  
  Solution...
  
  ```
                      if($entry->relatedContent){
                          $index = -1;
                          foreach ($entry->relatedContent->all() as $block) {
                              switch ($block->type->handle) {
                                  case 'relateContent':
                                      $index++;
                                      $relatedFeaturedEntries =  $block->featured->first();
                                      $relationsData[$index] = [
                                          'title' => $relatedFeaturedEntries->title,
                                          'url' => $relatedFeaturedEntries->url
                                      ];
  
                                      $relationsData[$index]['subItems'] = [];
                                      $relatedSubEntries =  $block->related->all();
                                      foreach ($relatedSubEntries as $relatedSubEntry) {
                                          $relationsData[$index]['subItems'][] = [
                                              'title' => $relatedSubEntry->title,
                                              'url' => $relatedSubEntry->url
                                          ];
                                      }
                                  break;
                              }
                          }
                      }
  
  ```
  
  
  ---
  
  