Answers.Body: <p>Both approaches are valid. In your first example, you could have moved <code>.all()</code> into the for-loop; and in the second example, you could have moved <code>.all()</code> out of the for-loop, and into the <code>{% set entries = ... %}</code> statement.</p>
  
  <p>Until you call <code>.all()</code>, you are working with an <a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html" rel="nofollow noreferrer">entry query</a>, and after you call <code>.all()</code>, you’re working with an array of the actual entries (the query results).</p>
  
  <p>So which approach is <em>better</em> depends on what else you’re doing in your template.</p>
  
  <p>For example, if you need to output the total number of entries, you could do this:</p>
  
  
  
  <pre class="lang-twig prettyprint-override"><code>{% set entryQuery = craft.entries()
      .section('news')
  %}
  
  {% for entry in entryQuery.all() %}
      ...
  {% endfor %}
  
  Total: {{ entryQuery.count() }}
  </code></pre>
  
  <p>But it would result in two database queries getting executed – once to get the entries for the for-loop, and a second query to get the total matching entries.</p>
  
  <p>If you know you’re going to need that info, then it would be better to just fetch the entries ahead of time, and check the length of the array instead:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set entries = craft.entries()
      .section('news')
      .all()
  %}
  
  {% for entry in entries %}
      ...
  {% endfor %}
  
  Total: {{ entries|length }}
  </code></pre>
  
  <p>Regardless of the approach you decide to go with, you might want to make sure you are using semantec variable names. In your two examples you have <code>events</code> (an array of actual event entries) and <code>entries</code> (an entry query).</p>
  
  <p>I would say that if you are not going to call <code>.all()</code> when defining your <code>entries</code> variable, then you might want to rename that to either <code>entryQuery</code> or <code>query</code>, just so it’s easier to remember what sort of variable you’re working with later on in the template.</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set entryQuery = craft.entries()
      .section('news')
      .orderBy('postDate desc')
      .limit('4')
      .with(['imageGallery'])
  %}
  
  {% for entry in entryQuery.all() %}
      ...
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-13T21:27:13.877Z
Answers.Text: Damn, tied... you clearly put in a bit more effort. :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-14T08:07:31.567Z
Answers.Text: Thanks for the detailed response Brandon. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net
Answers.UserDisplayName: Paul Frost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-14T14:08:26.483Z
Answers.Text: Yeah, we’re slowly working to add better examples to the docs. I just rewrote the [Element Queries](https://docs.craftcms.com/v3/dev/element-queries/) page to hopefully be a little more clear, and with more usable examples.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-13T21:24:54.373Z
Answers.LastActivityDate: 2018-08-13T21:24:54.373Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p>They are both correct.</p>
  
  <p>In your first example, <code>events</code> is an <em>array of entries</em>. In your second example, <code>entries</code> is an <em>Element Query</em>. As soon as you apply the <code>.all()</code> to your Element Query, it <a href="https://docs.craftcms.com/api/v3/craft-elements-db-elementquery.html#method-all" rel="nofollow noreferrer">converts that into</a> an array of entries.</p>
  
  <p>The only two things you need to know about <code>.all()</code> is:</p>
  
  <ul>
  <li>It must exist (or use <code>.one()</code>)</li>
  <li>It must be last</li>
  </ul>
  
  <p>That's it!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-14T07:55:14.377Z
Answers.Text: Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.
Answers.UserDisplayName: Paul Frost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-13T21:26:38.433Z
Answers.LastActivityDate: 2018-08-13T21:26:38.433Z
Answers.UserDisplayName: Lindsey D
Body: <p>I'm confused, I have ended up with 2 similar queries in a template after converting them from Craft 2.</p>
  
  <pre><code>{% set events = craft.entries()
      .section('events')
      .eventDate('&gt;' ~ now|date('Y-m-d H:i:s'))
      .limit(3)
      .orderby('eventDate')
      .all()
  %}
  {% for entry in events %}
  ...
  {% endfor %}
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{% set entries = craft.entries()
      .section('news')
      .orderBy('postDate desc')
      .limit('4')
      .with(['imageGallery'])
  %}
  {% for entry in entries.all() %}
  ...
  {% endfor %}
  </code></pre>
  
  <p>Both work without any errors reported but which one is better or correct?</p>
  
Tags: <craft3><query><queries>
Title: Where should the .all() be placed in Craft 3
ThreadText: # Where should the .all() be placed in Craft 3
  
  (Asked by: username_0 on 2018-08-13)
  
  I'm confused, I have ended up with 2 similar queries in a template after converting them from Craft 2.
  
  ```
  {% set events = craft.entries()
      .section('events')
      .eventDate('>' ~ now|date('Y-m-d H:i:s'))
      .limit(3)
      .orderby('eventDate')
      .all()
  %}
  {% for entry in events %}
  ...
  {% endfor %}
  
  ```
  
  and
  
  ```
  {% set entries = craft.entries()
      .section('news')
      .orderBy('postDate desc')
      .limit('4')
      .with(['imageGallery'])
  %}
  {% for entry in entries.all() %}
  ...
  {% endfor %}
  
  ```
  
  Both work without any errors reported but which one is better or correct?
  
  **Tags:** craft3, query, queries
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-08-13. Score: 4 | Accepted answer
  
  Both approaches are valid. In your first example, you could have moved `.all()` into the for-loop; and in the second example, you could have moved `.all()` out of the for-loop, and into the `{% set entries = ... %}` statement.
  
  Until you call `.all()`, you are working with an [entry query](https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html), and after you call `.all()`, you’re working with an array of the actual entries (the query results).
  
  So which approach is *better* depends on what else you’re doing in your template.
  
  For example, if you need to output the total number of entries, you could do this:
  
  ```twig
  {% set entryQuery = craft.entries()
      .section('news')
  %}
  
  {% for entry in entryQuery.all() %}
      ...
  {% endfor %}
  
  Total: {{ entryQuery.count() }}
  
  ```
  
  But it would result in two database queries getting executed – once to get the entries for the for-loop, and a second query to get the total matching entries.
  
  If you know you’re going to need that info, then it would be better to just fetch the entries ahead of time, and check the length of the array instead:
  
  ```twig
  {% set entries = craft.entries()
      .section('news')
      .all()
  %}
  
  {% for entry in entries %}
      ...
  {% endfor %}
  
  Total: {{ entries|length }}
  
  ```
  
  Regardless of the approach you decide to go with, you might want to make sure you are using semantec variable names. In your two examples you have `events` (an array of actual event entries) and `entries` (an entry query).
  
  I would say that if you are not going to call `.all()` when defining your `entries` variable, then you might want to rename that to either `entryQuery` or `query`, just so it’s easier to remember what sort of variable you’re working with later on in the template.
  
  ```twig
  {% set entryQuery = craft.entries()
      .section('news')
      .orderBy('postDate desc')
      .limit('4')
      .with(['imageGallery'])
  %}
  
  {% for entry in entryQuery.all() %}
      ...
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_2**: Damn, tied... you clearly put in a bit more effort. :)  
  > **username_0**: Thanks for the detailed response username_1. It helps with understanding the process, which is something I find very hard with Craft. Sometimes I long for the simplicity of EE templates and the lack of complexity/code needed for simple thngs, but I do recognise that Craft allows me to do much more complicated things that I couldn't do in EE. It would be nice if there were more Craft 3 complete template examples for common tasks, like the ones at the bottom of the official docs an on craftcookbook.net  
  > **username_1**: Yeah, we’re slowly working to add better examples to the docs. I just rewrote the [Element Queries](https://docs.craftcms.com/v3/dev/element-queries/) page to hopefully be a little more clear, and with more usable examples.  
  
  ---
  
  ### Answer by username_2 on 2018-08-13. Score: 2
  
  They are both correct.
  
  In your first example, `events` is an *array of entries*. In your second example, `entries` is an *Element Query*. As soon as you apply the `.all()` to your Element Query, it [converts that into](https://docs.craftcms.com/api/v3/craft-elements-db-elementquery.html#method-all) an array of entries.
  
  The only two things you need to know about `.all()` is:
  
  * It must exist (or use `.one()`)
  * It must be last
  
  That's it!
  
  > **Comments:**
  > **username_0**: Brad wins due to the detailed explanation which helps me understand what's going on, but you get a special mention for the easy to remember rules.  
  
  ---
  
  