Answers.Body: <p>Sounds like you need to do some <a href="https://craftcms.com/docs/templating/eager-loading-elements" rel="nofollow noreferrer">eager loading</a> of your assets.</p>
  
  <p>Here are some examples of how to do that in the Element API plugin:</p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/15220/elementapi-handling-categorymodel-and-returning-a-heirachical-object/15228#15228">ElementAPI handling CategoryModel and returning a heirachical object</a></p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/18173/getting-categories-in-elementapi-performance-issues-and-help-required/18177#18177">Getting categories in elementAPI performance issues and help required</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-08T07:29:04.093Z
Answers.Text: Those articles were helpful, thanks Brad! I think I got it working now, but I ended up removing the `$artwork` bc I didn't need it in the end.  And for some reason I had to remove the `->find()`. Load time decreased by 2/3!
Answers.UserDisplayName: Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-06T23:27:07.740Z
Answers.LastActivityDate: 2017-03-06T23:27:07.740Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm producing a list of entries that each use an image asset from a related entry.  I see <a href="https://craftcms.stackexchange.com/a/13236/4433">in this ticket</a> how to get an asset's data, and I've combined that with how I learned to output an object's data from a <a href="https://mijingo.com/lessons/creating-an-element-api-in-craft-cms/" rel="nofollow noreferrer">Mijingo video</a> (via the <code>array_map</code> function).  </p>
  
  <p>This approach works, but seeing how there are about 130 entries in the section, the load time is a whopping 2.31s versus when I take out the related entry's asset code, which then results in a much smaller 500ms load time.  Is there a better way to access this data?</p>
  
  <p>Here's the code I'm using:</p>
  
  <pre><code>return [
      'endpoints' =&gt; [
          'api/pressEntries.json' =&gt; [
              'elementType' =&gt; 'Entry',
              'criteria' =&gt; ['section' =&gt; 'pressEntries'],
              'transformer' =&gt; function(EntryModel $entry) {
                  return [
                      'id'                =&gt; $entry-&gt;id,
                      'title'             =&gt; $entry-&gt;title,
                      'slug'              =&gt; $entry-&gt;slug,
                      'postDate'          =&gt; $entry-&gt;postDate,
                      'type'              =&gt; $entry-&gt;type-&gt;name,
                      'url'               =&gt; $entry-&gt;url,
                      'jsonUrl'           =&gt; UrlHelper::getUrl("api/press/{$entry-&gt;id}.json"),
                      'relatedRecording'  =&gt; array_map(function(EntryModel $album) {
                          $data = [
                              'id'    =&gt; $album-&gt;id,
                              'title' =&gt; $album-&gt;title,
                          ];
  
                          $artwork = $album-&gt;recordingArtwork-&gt;first();
                          if ($artwork) {
                              $data['artwork'] = $artwork-&gt;url;
                          }
                          return $data;
  
                      }, $entry-&gt;relatedRecording-&gt;find()),
                  ];
              },
          ]
      ]
  ];
  </code></pre>
  
Tags: <elementcriteriamodel><plugin-elementapi>
Title: Element API: return a related entry's asset data with entry data
ThreadText: # Element API: return a related entry's asset data with entry data
  
  (Asked by: username_0 on 2017-03-06)
  
  I'm producing a list of entries that each use an image asset from a related entry. I see [in this ticket](https://craftcms.stackexchange.com/a/13236/4433) how to get an asset's data, and I've combined that with how I learned to output an object's data from a [Mijingo video](https://mijingo.com/lessons/creating-an-element-api-in-craft-cms/) (via the `array_map` function).
  
  This approach works, but seeing how there are about 130 entries in the section, the load time is a whopping 2.31s versus when I take out the related entry's asset code, which then results in a much smaller 500ms load time. Is there a better way to access this data?
  
  Here's the code I'm using:
  
  ```
  return [
      'endpoints' => [
          'api/pressEntries.json' => [
              'elementType' => 'Entry',
              'criteria' => ['section' => 'pressEntries'],
              'transformer' => function(EntryModel $entry) {
                  return [
                      'id'                => $entry->id,
                      'title'             => $entry->title,
                      'slug'              => $entry->slug,
                      'postDate'          => $entry->postDate,
                      'type'              => $entry->type->name,
                      'url'               => $entry->url,
                      'jsonUrl'           => UrlHelper::getUrl("api/press/{$entry->id}.json"),
                      'relatedRecording'  => array_map(function(EntryModel $album) {
                          $data = [
                              'id'    => $album->id,
                              'title' => $album->title,
                          ];
  
                          $artwork = $album->recordingArtwork->first();
                          if ($artwork) {
                              $data['artwork'] = $artwork->url;
                          }
                          return $data;
  
                      }, $entry->relatedRecording->find()),
                  ];
              },
          ]
      ]
  ];
  
  ```
  
  **Tags:** elementcriteriamodel, plugin-elementapi
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-03-06. Score: 1 | Accepted answer
  
  Sounds like you need to do some [eager loading](https://craftcms.com/docs/templating/eager-loading-elements) of your assets.
  
  Here are some examples of how to do that in the Element API plugin:
  
  [ElementAPI handling CategoryModel and returning a heirachical object](https://craftcms.stackexchange.com/questions/15220/elementapi-handling-categorymodel-and-returning-a-heirachical-object/15228#15228)
  
  [Getting categories in elementAPI performance issues and help required](https://craftcms.stackexchange.com/questions/18173/getting-categories-in-elementapi-performance-issues-and-help-required/18177#18177)
  
  > **Comments:**
  > **username_0**: Those articles were helpful, thanks username_1! I think I got it working now, but I ended up removing the `$artwork` bc I didn't need it in the end.  And for some reason I had to remove the `->find()`. Load time decreased by 2/3!  
  
  ---
  
  