<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>

CC BY-SA 4.0
2022-06-15T08:28:12.887Z
2022-06-15T08:35:45.397Z
2022-06-15T08:35:45.397Z
James Smith
<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>

CC BY-SA 4.0
2022-06-15T04:25:28.820Z
2022-06-15T04:25:28.820Z
Holly
<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>

<matrix><craft4>
How to get the image from the last matrix block using twig's last filter
# 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() %}


---

