<p>Yes you're correct, <code>one()</code> will get the first matching element. However, in your code you're not asking for the first image block, but rather the first image from each image block (because each image field can have multiple images attached to it).</p>

<p>The structure is basically something like this:</p>

<pre><code>entry          &lt;- 1. The entry
  mainPost     &lt;- 2. The matrix field
    postImage  &lt;- 3. The matrix field's block
      image    &lt;- 4. The field inside the matrix field's block
</code></pre>

<p>We want to get the first result from #3 but you're currently only asking for the first result from #4. (It is probably a good idea to also ask for the first result from #4 as well btw.)</p>

<p>I think something like the following would work in your case:</p>

<pre><code>{% for block in entry.mainPost.type("postImage").limit(1).all() %}
  {% set image = block.image.one() %}
  {% if image %}
    &lt;img src="{{ image.getUrl('summaryImage')}}" alt=""&gt;
  {% endif %}
{% endfor %}
</code></pre>

<p>UPDATE: looks like you actually need to use <code>limit()</code> instead of <code>one()</code> when querying for the block.</p>

<p>UPDATE 2: tacked on the <code>all()</code> to fix the deprecation!</p>

CC BY-SA 3.0
2018-04-11T22:53:33.227Z
@Robin thanks for the clarification. Lesson for us both!
Shad
CC BY-SA 3.0
2018-04-10T00:36:05.007Z
Thanks for the response! I added your code in place of what I had and received the following error message: Impossible to access an attribute ("image") on a string variable ("46").

If I remove .one() from entry.mainPost.type("postImage), my page loads, but without any images...
Shad
CC BY-SA 3.0
2018-04-10T00:38:06.940Z
The line number it gives as having the problem is 

{% set image = block.image.one() %}
Shad
CC BY-SA 3.0
2018-04-10T00:45:11.310Z
Oops lol - I'll blame it on how late it is. I updated my post - looks like you'll have to use `limit(1)` vs `one()`. I tested it locally on one of my sites and this seems to actually work. :P
Jalen Davenport
CC BY-SA 3.0
2018-04-10T00:49:37.893Z
Thanks again! And yes, that's working as expected now. However, as I'm using Craft 3, I'm getting the Deprecation Error "Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them."
Shad
CC BY-SA 3.0
2018-04-10T00:55:42.087Z
Ah yes, so just tack on `.all()` to the end and you should be good to go!
Jalen Davenport
CC BY-SA 3.0
2018-04-10T01:08:16.497Z
Fantastic! Thanks so much for your help Jalen!
Shad
CC BY-SA 3.0
2018-04-10T01:09:58.257Z
No worries. Thanks for bearing with all my edits and suggestions...
Jalen Davenport
CC BY-SA 3.0
2018-04-10T05:05:53.743Z
@jalen `limit(1).all()` and, `one()` is actually the same. The first just returns an array with 1 element, the last returns the element directly. There is no need to loop through one element. You could remove the loop. If all fields are required you could do `entry.mainPost.type("postImage").one().image.one().getUrl()`
Robin Schambach
CC BY-SA 3.0
2018-04-11T00:57:27.080Z
@RobinSchambach, I tried adding what you suggested, and while I'm not getting an error, neither is the image showing now...
Shad
CC BY-SA 3.0
2018-04-11T05:03:10.490Z
@Shad that wasn't a suggestion for you but a hint for Jalen how `one` and `all` works
Robin Schambach
CC BY-SA 3.0
2018-04-10T00:13:12.957Z
2018-04-10T00:55:24.440Z
2018-04-10T00:55:24.440Z
Jalen Davenport
<p>I'm sure I'm doing something wrong here, but I'm at a loss as to what. I'm using Craft 3. My travel entry has 7 fields, one of those being a matrix field. My matrix has two blocks, <code>postText</code> and <code>postImage</code>.</p>

<p>Here's the bit of code I'm working with:</p>

<pre><code>{% for block in entry.mainPost.all() %}
  {% if block.type == "postImage" %}
    {% set image = block.image.one() %}
        {% if image %}
            &lt;img src="{{ image.getUrl('summaryImage')}}" alt=""&gt;
        {% endif %}
  {% endif %}
{% endfor %}
</code></pre>

<p>I have an entry that has two images in the Matrix field. When I load the page, both images load. The documentation states that using <code>.one()</code> <em>"Returns the first matching element"</em>. I have to assume that I'm not understanding the ElementCriteriaModel or Assets or the way Matrix works or something.</p>

<assets><matrix><image>
block.image.one() shows more than one image
# block.image.one() shows more than one image

(Asked by: username_0 on 2018-04-09)

I'm sure I'm doing something wrong here, but I'm at a loss as to what. I'm using Craft 3. My travel entry has 7 fields, one of those being a matrix field. My matrix has two blocks, postText and postImage.
Here's the bit of code I'm working with:
{% for block in entry.mainPost.all() %}
  {% if block.type == "postImage" %}
    {% set image = block.image.one() %}
        {% if image %}
            
        {% endif %}
  {% endif %}
{% endfor %}

I have an entry that has two images in the Matrix field. When I load the page, both images load. The documentation states that using .one() "Returns the first matching element". I have to assume that I'm not understanding the ElementCriteriaModel or Assets or the way Matrix works or something.

**Tags:** assets, matrix, image


---

## 1 answers

### Answer by username_1 on 2018-04-10. Score: 3 | Accepted answer

Yes you're correct, one() will get the first matching element. However, in your code you're not asking for the first image block, but rather the first image from each image block (because each image field can have multiple images attached to it).
The structure is basically something like this:
entry          <- 1. The entry
  mainPost     <- 2. The matrix field
    postImage  <- 3. The matrix field's block
      image    <- 4. The field inside the matrix field's block

We want to get the first result from #3 but you're currently only asking for the first result from #4. (It is probably a good idea to also ask for the first result from #4 as well btw.)
I think something like the following would work in your case:
{% for block in entry.mainPost.type("postImage").limit(1).all() %}
  {% set image = block.image.one() %}
  {% if image %}
    
  {% endif %}
{% endfor %}

UPDATE: looks like you actually need to use limit() instead of one() when querying for the block.
UPDATE 2: tacked on the all() to fix the deprecation!

> **Comments:**
> **username_0**: Thanks for the response! I added your code in place of what I had and received the following error message: Impossible to access an attribute ("image") on a string variable ("46").  If I remove .one() from entry.mainPost.type("postImage), my page loads, but without any images...  
> **username_0**: The line number it gives as having the problem is   {% set image = block.image.one() %}  
> **username_1**: Oops lol - I'll blame it on how late it is. I updated my post - looks like you'll have to use `limit(1)` vs `one()`. I tested it locally on one of my sites and this seems to actually work. :P  
> **username_0**: Thanks again! And yes, that's working as expected now. However, as I'm using Craft 3, I'm getting the Deprecation Error "Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them."  
> **username_1**: Ah yes, so just tack on `.all()` to the end and you should be good to go!  
> **username_0**: Fantastic! Thanks so much for your help Jalen!  
> **username_1**: No worries. Thanks for bearing with all my edits and suggestions...  
> **username_2**: @jalen `limit(1).all()` and, `one()` is actually the same. The first just returns an array with 1 element, the last returns the element directly. There is no need to loop through one element. You could remove the loop. If all fields are required you could do `entry.mainPost.type("postImage").one().image.one().getUrl()`  
> **username_0**: @RobinSchambach, I tried adding what you suggested, and while I'm not getting an error, neither is the image showing now...  
> **username_2**: @username_0 that wasn't a suggestion for you but a hint for Jalen how `one` and `all` works  
> **username_0**: @Robin thanks for the clarification. Lesson for us both!  

---

