Answers.Body: <p>Your original (Craft 3?) code should have been throwing deprecation errors in the control panel (Utilities &gt; Deprecation Errors), because you haven't executed your Element Query (<code>entry.homeHeroBanner</code>). It should be:</p>
  <pre><code>{% set lastBlock = entry.homeHeroBanner.all()|last %}
  or: {% set lastBlock = entry.homeHeroBanner.inReverse().one() %}
  
  {% set lastImage = lastBlock.image.one() %}
  </code></pre>
  <p>or preferably, with Eager Loading:</p>
  <pre><code>{% set lastBlock = entry.homeHeroBanner.with('image').all()|last %}
  {% set lastImage = lastBlock.image[0] %}
  </code></pre>
  <p>Strangely enough I answered the same question yesterday - here's a more complete explanation: <a href="https://craftcms.stackexchange.com/a/39528/271">https://craftcms.stackexchange.com/a/39528/271</a></p>
  <p>Before upgrading to Craft 4 be sure to correct any deprecation errors.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T08:28:12.887Z
Answers.LastActivityDate: 2022-06-15T08:35:45.397Z
Answers.LastEditDate: 2022-06-15T08:35:45.397Z
Answers.UserDisplayName: James Smith
Answers.Body: <p>Found how to do it on Craft 4.</p>
  <pre><code>{% set numberOfBlocks = entry.homeHeroBanner.count() %}
  {% set lastBlock = entry.homeHeroBanner.all()[numberOfBlocks - 1] %}
  {% set lastImage = lastBlock.backgroundImage.one() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T04:25:28.820Z
Answers.LastActivityDate: 2022-06-15T04:25:28.820Z
Answers.UserDisplayName: Holly
Body: <p>I am on Craft CMS version 4.0.4. I used the <code>last</code> filter to get the last matrix, but when I tried to get the image, I kept getting an error:</p>
  <blockquote>
  <p>Impossible to access an attribute (&quot;image&quot;) on a string variable
  (&quot;}&quot;).</p>
  </blockquote>
  <p>Here's my code</p>
  <pre><code>{% set lastBlock = entry.homeHeroBanner|last %}
  {% set lastImage = lastBlock.image.first() %}
  </code></pre>
  <p>Anyone know how to get the image from the last matrix block?</p>
  
Tags: <matrix><craft4>
Title: How to get the image from the last matrix block using twig's last filter
ThreadText: # How to get the image from the last matrix block using twig's last filter
  
  (Asked by: username_0 on 2022-06-15)
  
  I am on Craft CMS version 4.0.4. I used the `last` filter to get the last matrix, but when I tried to get the image, I kept getting an error:
  
  > Impossible to access an attribute ("image") on a string variable
  > ("}").
  
  Here's my code
  
  ```
  {% set lastBlock = entry.homeHeroBanner|last %}
  {% set lastImage = lastBlock.image.first() %}
  
  ```
  
  Anyone know how to get the image from the last matrix block?
  
  **Tags:** matrix, craft4
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-06-15. Score: 1
  
  Your original (Craft 3?) code should have been throwing deprecation errors in the control panel (Utilities > Deprecation Errors), because you haven't executed your Element Query (`entry.homeHeroBanner`). It should be:
  
  ```
  {% set lastBlock = entry.homeHeroBanner.all()|last %}
  or: {% set lastBlock = entry.homeHeroBanner.inReverse().one() %}
  
  {% set lastImage = lastBlock.image.one() %}
  
  ```
  
  or preferably, with Eager Loading:
  
  ```
  {% set lastBlock = entry.homeHeroBanner.with('image').all()|last %}
  {% set lastImage = lastBlock.image[0] %}
  
  ```
  
  Strangely enough I answered the same question yesterday - here's a more complete explanation: <https://craftcms.stackexchange.com/a/39528/271>
  
  Before upgrading to Craft 4 be sure to correct any deprecation errors.
  
  
  ---
  
  ### Answer by username_0 on 2022-06-15. Score: 1
  
  Found how to do it on Craft 4.
  
  ```
  {% set numberOfBlocks = entry.homeHeroBanner.count() %}
  {% set lastBlock = entry.homeHeroBanner.all()[numberOfBlocks - 1] %}
  {% set lastImage = lastBlock.backgroundImage.one() %}
  
  ```
  
  
  ---
  
  