Answers.Body: <p>It's because <code>craft.entries.id(2)</code> could be an object/array containing many entries. <code>.first()</code> gets the first object within it, which in your case is the 1 entry.</p>
  
  <p>Edit*: I'm wrong, see the comment beneath.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-19T17:06:14.277Z
Answers.Text: Very close, but not entirely accurate. Without the `first`, you're actually dealing with an **ElementCriteriaModel**. See [this question/answer](http://craftcms.stackexchange.com/a/16070/45) for more details.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-18T13:50:20.707Z
Answers.Text: I have a followup question: given that `.first()` will only work in some cases (`ElementCriteriaModel` when eager loading is not being used), is there a compelling reason to not just always use `[0]` in place of `.first()` given that it seems to work in all of the same situations, and also works with normal arrays?
Answers.UserDisplayName: cmal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-19T14:28:22.103Z
Answers.LastActivityDate: 2016-08-20T21:40:53.430Z
Answers.LastEditDate: 2016-08-20T21:40:53.430Z
Answers.UserDisplayName: joep
Answers.Body: <p>By default, when you query elements you get back an array of Elements. Craft assumes you are using <code>find()</code> to query your elements if you don't explicitly say otherwise. You can loop through those elements and display attributes of those elements like you are doing:</p>
  
  <pre><code>{% for entry in craft.entries.find() %}
      {{ entry.title }}
  {% endfor %}
  </code></pre>
  
  <p>More generally, here an abstract example of what you are getting back from your query:</p>
  
  <pre><code>{# A query for several Entry elements #}
  craft.entries.find()
  
  {# Results are an array of Entry Models #}
  array(
    EntryModel,
    EntryModel,
    EntryModel
  )
  </code></pre>
  
  <p>And in the case, you are only querying a single entry, you still get it back in the format of an array when using the default <code>find()</code> behavior.</p>
  
  <pre><code>{# A query for a single Entry element using find() #}
  craft.entries.id(11).find()
  
  {# Results are an array of a single Entry Model #}
  array(
    EntryModel
  )
  </code></pre>
  
  <p>However, if you update your query to use <code>first()</code>, you get back the Model directly:</p>
  
  <pre><code>{# A query for a single Entry element using first() #}
  craft.entries.id(11).first()
  
  {# Results are the Entry Model itself (not within an array) #}
  EntryModel
  </code></pre>
  
  <hr>
  
  <p>There is one more scenario to consider when using <code>first()</code>. If you happen to be <a href="https://craftcms.com/docs/templating/eager-loading-elements">Eager Loading Elements</a>, the results that you get back for your Element fields are not ElementCriteriaModels, but just normal arrays. So, in the case of accessing the data returned from Eager Loaded Elements you will not be able to use <code>first()</code> as the <code>first()</code> method is only available when you interact with ElementCriteriaModels.</p>
  
  <p>In these cases, or in cases where you are sharing a partial template between two pages, one which uses eager loading and one that doesn't, you will need to access your first entry in an array using array syntax.</p>
  
  <pre><code>{% set entry = entry.entryRelationsFieldHandle[0] ?? null %}
  </code></pre>
  
  <p>In the above example, since we are accessing the first element in the array directly <code>[0]</code>, if it doesn't exist, we will get an error (The first <code>first()</code> method is a bit smarter and will not throw an error). So, in the above example we have to explicitly plan for the situation of having no results using the null-coalescing operator <code>??</code> and falling back to null. </p>
  
  <p>A longer form discussion of this scenario can be found in the article <a href="https://straightupcraft.com/articles/examples-of-eager-loading-elements-in-twig-and-php">Examples of Eager Loading Elements in Twig and PHP</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-20T18:34:20.140Z
Answers.LastActivityDate: 2016-08-20T18:34:20.140Z
Answers.UserDisplayName: Ben Parizek
Body: <p>Can someone explain why and when we need to use <code>.first()</code>?</p>
  
  <p>An example to use would be this code.</p>
  
  <pre><code>{% set featuredProductsHome = craft.entries.id(2).first() %}
  {% for product in featuredProductsHome.featuredProducts %}
      {% import '_macros/_product' as m_product %}
      {{ m_product.teaser(product, 500, "product-teaser--featured") }}
  {% endfor %}
  </code></pre>
  
  <p>The above works, but when I had <code>{% set featuredProductsHome = craft.entries.id(2) %}</code> without calling <code>.first()</code> it returned nothing.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-20T18:41:37.447Z
Comments.Text: Just for kicks, since you are limiting your query to a single entry result with `id(2)` you could also use `last()` and should see the same working results as when you use `first()`.
Comments.UserDisplayName: Ben Parizek
Tags: <templating>
Title: When to use .first() and why?
ThreadText: # When to use .first() and why?
  
  (Asked by: username_0 on 2016-08-19)
  
  Can someone explain why and when we need to use `.first()`?
  
  An example to use would be this code.
  
  ```
  {% set featuredProductsHome = craft.entries.id(2).first() %}
  {% for product in featuredProductsHome.featuredProducts %}
      {% import '_macros/_product' as m_product %}
      {{ m_product.teaser(product, 500, "product-teaser--featured") }}
  {% endfor %}
  
  ```
  
  The above works, but when I had `{% set featuredProductsHome = craft.entries.id(2) %}` without calling `.first()` it returned nothing.
  
  **Tags:** templating
  
  > **Comments:**
  > **username_1**: Just for kicks, since you are limiting your query to a single entry result with `id(2)` you could also use `last()` and should see the same working results as when you use `first()`.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-08-20. Score: 7 | Accepted answer
  
  By default, when you query elements you get back an array of Elements. Craft assumes you are using `find()` to query your elements if you don't explicitly say otherwise. You can loop through those elements and display attributes of those elements like you are doing:
  
  ```
  {% for entry in craft.entries.find() %}
      {{ entry.title }}
  {% endfor %}
  
  ```
  
  More generally, here an abstract example of what you are getting back from your query:
  
  ```
  {# A query for several Entry elements #}
  craft.entries.find()
  
  {# Results are an array of Entry Models #}
  array(
    EntryModel,
    EntryModel,
    EntryModel
  )
  
  ```
  
  And in the case, you are only querying a single entry, you still get it back in the format of an array when using the default `find()` behavior.
  
  ```
  {# A query for a single Entry element using find() #}
  craft.entries.id(11).find()
  
  {# Results are an array of a single Entry Model #}
  array(
    EntryModel
  )
  
  ```
  
  However, if you update your query to use `first()`, you get back the Model directly:
  
  ```
  {# A query for a single Entry element using first() #}
  craft.entries.id(11).first()
  
  {# Results are the Entry Model itself (not within an array) #}
  EntryModel
  
  ```
  
  ---
  
  There is one more scenario to consider when using `first()`. If you happen to be [Eager Loading Elements](https://craftcms.com/docs/templating/eager-loading-elements), the results that you get back for your Element fields are not ElementCriteriaModels, but just normal arrays. So, in the case of accessing the data returned from Eager Loaded Elements you will not be able to use `first()` as the `first()` method is only available when you interact with ElementCriteriaModels.
  
  In these cases, or in cases where you are sharing a partial template between two pages, one which uses eager loading and one that doesn't, you will need to access your first entry in an array using array syntax.
  
  ```
  {% set entry = entry.entryRelationsFieldHandle[0] ?? null %}
  
  ```
  
  In the above example, since we are accessing the first element in the array directly `[0]`, if it doesn't exist, we will get an error (The first `first()` method is a bit smarter and will not throw an error). So, in the above example we have to explicitly plan for the situation of having no results using the null-coalescing operator `??` and falling back to null.
  
  A longer form discussion of this scenario can be found in the article [Examples of Eager Loading Elements in Twig and PHP](https://straightupcraft.com/articles/examples-of-eager-loading-elements-in-twig-and-php).
  
  
  ---
  
  ### Answer by username_2 on 2016-08-19. Score: 3
  
  It's because `craft.entries.id(2)` could be an object/array containing many entries. `.first()` gets the first object within it, which in your case is the 1 entry.
  
  Edit\*: I'm wrong, see the comment beneath.
  
  > **Comments:**
  > **username_3**: Very close, but not entirely accurate. Without the `first`, you're actually dealing with an **ElementCriteriaModel**. See [this question/answer](http://craftcms.stackexchange.com/a/16070/45) for more details.  
  > **username_4**: I have a followup question: given that `.first()` will only work in some cases (`ElementCriteriaModel` when eager loading is not being used), is there a compelling reason to not just always use `[0]` in place of `.first()` given that it seems to work in all of the same situations, and also works with normal arrays?  
  
  ---
  
  