Answers.Body: <blockquote>
  <p>In the returned object I'm expecting to see an array somewhere for custom field values listing the asset Id's, but it's drawing a blank</p>
  </blockquote>
  <p>The reason for this is that entry fields are lazy-loaded, meaning Craft only queries the values from the database once you access them using their field handle (or execute the entity query you get through the field handle, like with the asset field). You can change this using <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html#accessing-eager-loaded-elements" rel="nofollow noreferrer">Eager-Loading</a>.</p>
  <p>Without eager-loading, to get the asset you need to use the field handle (<code>featureImage</code>, in your case) to get an <a href="https://docs.craftcms.com/api/v3/craft-elements-db-assetquery.html" rel="nofollow noreferrer">AssetQuery</a>. Then you can use that to get the images (assets) in your field.</p>
  <pre class="lang-php prettyprint-override"><code>$entry = Entry::find() 
    -&gt;slug('0002')
    -&gt;one();
  
  /** @var \craft\elements\Asset */
  $featureImage = $entry-&gt;featureImage-&gt;one();
  
  $featureImageId = $featureImage-&gt;id;
  </code></pre>
  <p>See <a href="https://craftcms.com/docs/3.x/assets-fields.html#working-with-assets-field-data" rel="nofollow noreferrer">Working with assets field data</a> and <a href="https://craftcms.com/docs/3.x/assets.html#querying-assets" rel="nofollow noreferrer">Querying assets</a>. The object returned by <a href="https://docs.craftcms.com/api/v3/craft-db-query.html#method-one" rel="nofollow noreferrer">AssetQuery::one()</a> is an instance of <a href="https://docs.craftcms.com/api/v3/craft-elements-asset.html" rel="nofollow noreferrer">\craft\elements\Asset</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-07T05:41:16.817Z
Answers.Text: Ok, that is interesting. Thank you. How come other entry fields though are being loaded in this lazy-load? In the object I pasted above there is a geopolygon array there that has been loaded. Or does it just apply to assets?
Answers.UserDisplayName: Robert
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-07T07:55:02.430Z
Answers.Text: @Robert Not sure, it might depend on the fieldtype.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-06T07:48:28.703Z
Answers.LastActivityDate: 2021-04-06T07:48:28.703Z
Answers.UserDisplayName: MoritzLost
Body: <p>I want to get the id of an asset I've set for a feature image on an entry.</p>
  <p>To find the entry I'm using the slug as the criteria like so:</p>
  <pre><code>$entry = Entry::find() 
    -&gt;slug('0002')
    -&gt;one();
  </code></pre>
  <p>In the returned object I'm expecting to see an array somewhere for custom field values listing the asset Id's, but it's drawing a blank</p>
  <pre><code>[_behaviors:yii\base\Component:private] =&gt; Array
          (
              [customFields] =&gt; craft\behaviors\CustomFieldBehavior Object
                  (
                      [hasMethods] =&gt; 
                      [summary] =&gt; 
                      [featureImage] =&gt; 
                      [geopolygon] =&gt; [{&quot;col1&quot;:&quot;11&quot;,&quot;col2&quot;:&quot;22&quot;},{&quot;col1&quot;:&quot;11&quot;,&quot;col2&quot;:&quot;2&quot;},{&quot;col1&quot;:&quot;3&quot;,&quot;col2&quot;:&quot;2&quot;},{&quot;col1&quot;:&quot;&quot;,&quot;col2&quot;:&quot;&quot;}]
                      [_customFieldValues:craft\behaviors\CustomFieldBehavior:private] =&gt; Array
                          (
                          )
  
                      [owner] =&gt; craft\elements\Entry Object
   *RECURSION*
                      [_attachedEvents:yii\base\Behavior:private] =&gt; Array
                          (
                          )
  
                  )
  
          )
  
  </code></pre>
  <p>I know this entry does have a featureImage because it shows up in the admin and I can also view it on the frontend. I can also see all other custom values I've saved for the entry. What am I missing here?</p>
  
Tags: <assets>
Title: Getting assets from an entry programatically
ThreadText: # Getting assets from an entry programatically
  
  (Asked by: username_0 on 2021-04-06)
  
  I want to get the id of an asset I've set for a feature image on an entry.
  
  To find the entry I'm using the slug as the criteria like so:
  
  ```
  $entry = Entry::find() 
    ->slug('0002')
    ->one();
  
  ```
  
  In the returned object I'm expecting to see an array somewhere for custom field values listing the asset Id's, but it's drawing a blank
  
  ```
  [_behaviors:yii\base\Component:private] => Array
          (
              [customFields] => craft\behaviors\CustomFieldBehavior Object
                  (
                      [hasMethods] => 
                      [summary] => 
                      [featureImage] => 
                      [geopolygon] => [{"col1":"11","col2":"22"},{"col1":"11","col2":"2"},{"col1":"3","col2":"2"},{"col1":"","col2":""}]
                      [_customFieldValues:craft\behaviors\CustomFieldBehavior:private] => Array
                          (
                          )
  
                      [owner] => craft\elements\Entry Object
   *RECURSION*
                      [_attachedEvents:yii\base\Behavior:private] => Array
                          (
                          )
  
                  )
  
          )
  
  ```
  
  I know this entry does have a featureImage because it shows up in the admin and I can also view it on the frontend. I can also see all other custom values I've saved for the entry. What am I missing here?
  
  **Tags:** assets
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-04-06. Score: 2 | Accepted answer
  
  > In the returned object I'm expecting to see an array somewhere for custom field values listing the asset Id's, but it's drawing a blank
  
  The reason for this is that entry fields are lazy-loaded, meaning Craft only queries the values from the database once you access them using their field handle (or execute the entity query you get through the field handle, like with the asset field). You can change this using [Eager-Loading](https://craftcms.com/docs/3.x/dev/eager-loading-elements.html#accessing-eager-loaded-elements).
  
  Without eager-loading, to get the asset you need to use the field handle (`featureImage`, in your case) to get an [AssetQuery](https://docs.craftcms.com/api/v3/craft-elements-db-assetquery.html). Then you can use that to get the images (assets) in your field.
  
  ```php
  $entry = Entry::find() 
    ->slug('0002')
    ->one();
  
  /** @var \craft\elements\Asset */
  $featureImage = $entry->featureImage->one();
  
  $featureImageId = $featureImage->id;
  
  ```
  
  See [Working with assets field data](https://craftcms.com/docs/3.x/assets-fields.html#working-with-assets-field-data) and [Querying assets](https://craftcms.com/docs/3.x/assets.html#querying-assets). The object returned by [AssetQuery::one()](https://docs.craftcms.com/api/v3/craft-db-query.html#method-one) is an instance of [\craft\elements\Asset](https://docs.craftcms.com/api/v3/craft-elements-asset.html).
  
  > **Comments:**
  > **username_0**: Ok, that is interesting. Thank you. How come other entry fields though are being loaded in this lazy-load? In the object I pasted above there is a geopolygon array there that has been loaded. Or does it just apply to assets?  
  > **username_1**: @username_0 Not sure, it might depend on the fieldtype.  
  
  ---
  
  