Answers.Body: <p>If you already know the custom field name then it is pretty straight forward:</p>
  
  <pre><code>$currentEntry = craft()-&gt;entries-&gt;getEntryById($id);
  $relatedEntries = $currentEntry-&gt;customFieldName;
  </code></pre>
  
  <p>Then you could either loop through each one:</p>
  
  <pre><code>foreach ($relatedEntries as $entry) {
      echo $entry-&gt;title;
  }
  </code></pre>
  
  <p>Or simply get the data from the first one:</p>
  
  <pre><code>echo $relatedEntries-&gt;first()-&gt;title;
  </code></pre>
  
  <p>Remember that all related entries come back as an array even if you have specified that the user can only select 1.</p>
  
  <p>Hope this helps</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-08T19:42:28.420Z
Answers.Text: It was not exactly what I was looking for, but it got me in the right direction.
Answers.UserDisplayName: Hubert
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-08T12:38:46.200Z
Answers.LastActivityDate: 2014-08-08T12:38:46.200Z
Answers.UserDisplayName: nfourtythree
Body: <p>If use the following code:</p>
  
  <pre class="lang-php prettyprint-override"><code>$currentEntry = craft()-&gt;entries-&gt;getEntryById($entryId);
  $currentContent = $currentEntry-&gt;getContent()-&gt;getAttributes();
  $currentAttributes = array();
  
  $fieldLayout = $currentEntry-&gt;getFieldLayout();
  foreach ($fieldLayout-&gt;getFields() as $fieldLayoutField) {
      $field = $fieldLayoutField-&gt;getField();
      if (isset($currentContent[$field-&gt;handle])) {
          $currentAttributes[$field-&gt;handle] = $currentContent[$field-&gt;handle];
      }
  }
  </code></pre>
  
  <p>It retrieves all data that I want, but I don't get the data from relation fields. If an asset or entry has been selected, I won't receive that data. The result from that field is always null.</p>
  
  <p>Does anyone know how to do this?</p>
  
Tags: <plugin-development><field>
Title: How do I get the data from a relation field?
ThreadText: # How do I get the data from a relation field?
  
  (Asked by: username_0 on 2014-08-08)
  
  If use the following code:
  
  ```php
  $currentEntry = craft()->entries->getEntryById($entryId);
  $currentContent = $currentEntry->getContent()->getAttributes();
  $currentAttributes = array();
  
  $fieldLayout = $currentEntry->getFieldLayout();
  foreach ($fieldLayout->getFields() as $fieldLayoutField) {
      $field = $fieldLayoutField->getField();
      if (isset($currentContent[$field->handle])) {
          $currentAttributes[$field->handle] = $currentContent[$field->handle];
      }
  }
  
  ```
  
  It retrieves all data that I want, but I don't get the data from relation fields. If an asset or entry has been selected, I won't receive that data. The result from that field is always null.
  
  Does anyone know how to do this?
  
  **Tags:** plugin-development, field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-08-08. Score: 1 | Accepted answer
  
  If you already know the custom field name then it is pretty straight forward:
  
  ```
  $currentEntry = craft()->entries->getEntryById($id);
  $relatedEntries = $currentEntry->customFieldName;
  
  ```
  
  Then you could either loop through each one:
  
  ```
  foreach ($relatedEntries as $entry) {
      echo $entry->title;
  }
  
  ```
  
  Or simply get the data from the first one:
  
  ```
  echo $relatedEntries->first()->title;
  
  ```
  
  Remember that all related entries come back as an array even if you have specified that the user can only select 1.
  
  Hope this helps
  
  > **Comments:**
  > **username_0**: It was not exactly what I was looking for, but it got me in the right direction.  
  
  ---
  
  