Answers.Body: <p><code>.all()</code> returns a collection of elements, which you can loop through with a for loop.</p>
  
  <p><code>.one()</code> returns a single element, so you cannot use a for loop to access it. </p>
  
  <p>In this case you should just store the entry like:</p>
  
  <p><code>{% set entry = craft.entries.section('thing').one() %}</code> and then access <code>{{ entry.title }}</code> afterwards.</p>
  
  <p>(Also keep in mind that entry is automatically populated for section URLs, so you may want to use another variable name)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-28T13:49:16.057Z
Answers.LastActivityDate: 2019-10-28T13:49:16.057Z
Answers.UserDisplayName: Jeremy Gimbel
Body: <p>I'm confused about the new <code>.one()</code> argument in craft 3</p>
  
  <p>If I want to grab all entries I write:</p>
  
  <pre><code>{% for entry in craft.entries.section('thing').all() %}
    {{ entry.title }}
  {% endfor %}
  </code></pre>
  
  <p>Alls is nice and well. 20 something entries are returned.</p>
  
  <p>But I want to fetch only the latest entry so instead of <code>.all()</code> I use <code>.one()</code> (which was formerly <code>.first()</code>)</p>
  
  <p>But when I write:</p>
  
  <pre><code>{% for entry in craft.entries.section('thing').one() %}
    {{ entry.title }}
  {% endfor %}
  </code></pre>
  
  <p>It returns the error: </p>
  
  <blockquote>
    <p>Impossible to access an attribute ("title") on a string variable
    ("3").</p>
  </blockquote>
  
  <p>When I write: </p>
  
  <pre><code>{% for entry in craft.entries.section('portfolioEntry').limit(1).all() %}
    {{ entry.title }}
  {% endfor %}
  </code></pre>
  
  <p>Then it works. But that's not the right way, is it?</p>
  
Tags: <templating><elementcriteriamodel><elementquery><twigextension>
Title: .first() vs. .one() in craft 3
ThreadText: # .first() vs. .one() in craft 3
  
  (Asked by: username_0 on 2019-10-28)
  
  I'm confused about the new `.one()` argument in craft 3
  
  If I want to grab all entries I write:
  
  ```
  {% for entry in craft.entries.section('thing').all() %}
    {{ entry.title }}
  {% endfor %}
  
  ```
  
  Alls is nice and well. 20 something entries are returned.
  
  But I want to fetch only the latest entry so instead of `.all()` I use `.one()` (which was formerly `.first()`)
  
  But when I write:
  
  ```
  {% for entry in craft.entries.section('thing').one() %}
    {{ entry.title }}
  {% endfor %}
  
  ```
  
  It returns the error:
  
  > Impossible to access an attribute ("title") on a string variable
  > ("3").
  
  When I write:
  
  ```
  {% for entry in craft.entries.section('portfolioEntry').limit(1).all() %}
    {{ entry.title }}
  {% endfor %}
  
  ```
  
  Then it works. But that's not the right way, is it?
  
  **Tags:** templating, elementcriteriamodel, elementquery, twigextension
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-10-28. Score: 5 | Accepted answer
  
  `.all()` returns a collection of elements, which you can loop through with a for loop.
  
  `.one()` returns a single element, so you cannot use a for loop to access it.
  
  In this case you should just store the entry like:
  
  `{% set entry = craft.entries.section('thing').one() %}` and then access `{{ entry.title }}` afterwards.
  
  (Also keep in mind that entry is automatically populated for section URLs, so you may want to use another variable name)
  
  
  ---
  
  