Answers.Body: <p>That's because you don't actually fetch the entry. In order to receive the entry you have to execute your <code>ElementCriteriaModel</code> with <code>first()</code> or <code>find()</code> otherwise Craft tries to json encode a query</p>
  
  <pre><code> $block-&gt;subPageDirect-&gt;first()
  </code></pre>
  
  <p>You should read the docs about <a href="https://craftcms.com/docs/relations" rel="nofollow noreferrer">relations</a> and the <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">Element Criteria Model</a> </p>
  
  <p>Btw stack exchange has a wonderful syntax highlighting please add your code directly the next time and not in a screenshot</p>
  
  <p>Edit:</p>
  
  <pre><code>function panelsMatrix($array) {
      $panelBlocks = [];
      foreach ($array as $block) {
          switch ($block-&gt;type-&gt;handle) {
              case 'panel':
                  $relatedEntries =  $block-&gt;subPageDirect-&gt;first();
                  $attributes = $relatedEntries? $relatedEntries-&gt;getAttributes() : [];
  
                  $panelBlocks[] = [
                      'heading' =&gt; (string) $block-&gt;heading,
                      'text' =&gt; (string) $block-&gt;text,
                      'image' =&gt; $block-&gt;image[0]-&gt;url,
                      'imagePosition' =&gt; $block-&gt;imagePosition,
                      'textBaseColour' =&gt; $block-&gt;textBaseColour,
                      'textHorizontalAlignment' =&gt; $block-&gt;textHorizontalAlignment,
                      'textVerticalAlignment' =&gt; $block-&gt;textVerticalAlignment,
                      'imageOffsetX' =&gt;$block-&gt;imageOffsetX,
                      'imageOffsetY' =&gt;$block-&gt;imageOffsetY,
                      'backgroundColour' =&gt; $block-&gt;backgroundColour,
                      'subPageDirect' =&gt; $attributes
                  ];
              break;
          }
      }
      return $panelBlocks;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T18:08:00.753Z
Answers.Text: I think the use of the screenshots is appropriate this time, it helps explaining his problem better. ☺
Answers.UserDisplayName: Rosana Ruiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T18:10:16.097Z
Answers.Text: Why should a screenshot be better than actual code that you can copy, paste and test. You can't copy the code from a screenshot, so I don't see any advantage
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-12T11:00:22.643Z
Answers.Text: Hi @Robin Schambach, thanks for this. I tried using your code however this didn't work. The only change I saw is that the data is returning null when an entry has NOT been selected. When an entry has been selected the data returns an empty object.
Answers.UserDisplayName: Filth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-12T11:11:14.813Z
Answers.Text: Of course you have to define the attributes you want to return from this element otherwise you'll receive an empty array. I would have postet the entire code but you only gave us a screenshot so copy&paste didn't work.
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-12T11:16:01.397Z
Answers.Text: Updated my question with code from my Element API.
Answers.UserDisplayName: Filth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T17:44:55.603Z
Answers.LastActivityDate: 2018-03-12T11:21:49.697Z
Answers.LastEditDate: 2018-03-12T11:21:49.697Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have a matrix with a field to set a Entries URL. Unfortunately the entry is not being returned, only an empty object. </p>
  
  <p>Here are my field settings below and element api code.  </p>
  
  <p>See below for the response within the json. I get an empty object? </p>
  
  <p>Why is no data coming through? </p>
  
  <p><a href="https://i.sstatic.net/q3W89.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/q3W89.png" alt="enter image description here"></a></p>
  
  <p>I'm fetching the data within a function like so: </p>
  
  <p><a href="https://i.sstatic.net/KqzZP.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/KqzZP.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.sstatic.net/fl0u0.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fl0u0.png" alt="enter image description here"></a></p>
  
  <p>Here is the code from my Element API:</p>
  
  <pre><code>function panelsMatrix($array) {
      $panelBlocks = [];
      foreach ($array as $block) {
          switch ($block-&gt;type-&gt;handle) {
              case 'panel':
                  $panelBlocks[] = [
                      'heading' =&gt; (string) $block-&gt;heading,
                      'text' =&gt; (string) $block-&gt;text,
                      'image' =&gt; $block-&gt;image[0]-&gt;url,
                      'imagePosition' =&gt; $block-&gt;imagePosition,
                      'textBaseColour' =&gt; $block-&gt;textBaseColour,
                      'textHorizontalAlignment' =&gt; $block-&gt;textHorizontalAlignment,
                      'textVerticalAlignment' =&gt; $block-&gt;textVerticalAlignment,
                      'imageOffsetX' =&gt;$block-&gt;imageOffsetX,
                      'imageOffsetY' =&gt;$block-&gt;imageOffsetY,
                      'backgroundColour' =&gt; $block-&gt;backgroundColour,
                      'subPageDirect' =&gt; $block-&gt;subPageDirect-&gt;first()
                  ];
              break;
          }
      }
      return $panelBlocks;
  }
  </code></pre>
  
Tags: <matrix><plugin-elementapi><entries-field>
Title: Element API Fetch Entries URL Within Matrix
ThreadText: # Element API Fetch Entries URL Within Matrix
  
  (Asked by: username_0 on 2018-03-09)
  
  I have a matrix with a field to set a Entries URL. Unfortunately the entry is not being returned, only an empty object.
  
  Here are my field settings below and element api code.
  
  See below for the response within the json. I get an empty object?
  
  Why is no data coming through?
  
  [![enter image description here](https://i.sstatic.net/q3W89.png)](https://i.sstatic.net/q3W89.png)
  
  I'm fetching the data within a function like so:
  
  [![enter image description here](https://i.sstatic.net/KqzZP.png)](https://i.sstatic.net/KqzZP.png)
  
  [![enter image description here](https://i.sstatic.net/fl0u0.png)](https://i.sstatic.net/fl0u0.png)
  
  Here is the code from my Element API:
  
  ```
  function panelsMatrix($array) {
      $panelBlocks = [];
      foreach ($array as $block) {
          switch ($block->type->handle) {
              case 'panel':
                  $panelBlocks[] = [
                      'heading' => (string) $block->heading,
                      'text' => (string) $block->text,
                      'image' => $block->image[0]->url,
                      'imagePosition' => $block->imagePosition,
                      'textBaseColour' => $block->textBaseColour,
                      'textHorizontalAlignment' => $block->textHorizontalAlignment,
                      'textVerticalAlignment' => $block->textVerticalAlignment,
                      'imageOffsetX' =>$block->imageOffsetX,
                      'imageOffsetY' =>$block->imageOffsetY,
                      'backgroundColour' => $block->backgroundColour,
                      'subPageDirect' => $block->subPageDirect->first()
                  ];
              break;
          }
      }
      return $panelBlocks;
  }
  
  ```
  
  **Tags:** matrix, plugin-elementapi, entries-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-09. Score: 1 | Accepted answer
  
  That's because you don't actually fetch the entry. In order to receive the entry you have to execute your `ElementCriteriaModel` with `first()` or `find()` otherwise Craft tries to json encode a query
  
  ```
   $block->subPageDirect->first()
  
  ```
  
  You should read the docs about [relations](https://craftcms.com/docs/relations) and the [Element Criteria Model](https://craftcms.com/docs/templating/elementcriteriamodel)
  
  Btw stack exchange has a wonderful syntax highlighting please add your code directly the next time and not in a screenshot
  
  Edit:
  
  ```
  function panelsMatrix($array) {
      $panelBlocks = [];
      foreach ($array as $block) {
          switch ($block->type->handle) {
              case 'panel':
                  $relatedEntries =  $block->subPageDirect->first();
                  $attributes = $relatedEntries? $relatedEntries->getAttributes() : [];
  
                  $panelBlocks[] = [
                      'heading' => (string) $block->heading,
                      'text' => (string) $block->text,
                      'image' => $block->image[0]->url,
                      'imagePosition' => $block->imagePosition,
                      'textBaseColour' => $block->textBaseColour,
                      'textHorizontalAlignment' => $block->textHorizontalAlignment,
                      'textVerticalAlignment' => $block->textVerticalAlignment,
                      'imageOffsetX' =>$block->imageOffsetX,
                      'imageOffsetY' =>$block->imageOffsetY,
                      'backgroundColour' => $block->backgroundColour,
                      'subPageDirect' => $attributes
                  ];
              break;
          }
      }
      return $panelBlocks;
  }
  
  ```
  
  > **Comments:**
  > **username_2**: I think the use of the screenshots is appropriate this time, it helps explaining his problem better. ☺  
  > **username_1**: Why should a screenshot be better than actual code that you can copy, paste and test. You can't copy the code from a screenshot, so I don't see any advantage  
  > **username_0**: Hi @username_1, thanks for this. I tried using your code however this didn't work. The only change I saw is that the data is returning null when an entry has NOT been selected. When an entry has been selected the data returns an empty object.  
  > **username_1**: Of course you have to define the attributes you want to return from this element otherwise you'll receive an empty array. I would have postet the entire code but you only gave us a screenshot so copy&paste didn't work.  
  > **username_0**: Updated my question with code from my Element API.  
  
  ---
  
  