Answers.Body: <p>Instead of trying to loop through an object (your Query) you could just execute it</p>
  
  <pre><code>{% set component = {
      headline: headline ?? entry.headline ?? null,
      body: body ?? entry.body ?? null,
      items: items ?? entry.items.all() ?? null
  } %}
  
  &lt;h1&gt;{{ component.headline }}&lt;/h1&gt;
  &lt;p&gt;{{ component.body }}&lt;/p&gt;
  
  {% for item in component.items %}
      {{ item.name }}
  {% endfor %}
  </code></pre>
  
  <p>or if that doesn't fit your case, you can do it before</p>
  
  <pre><code>{% set relatedEntry = entry.callToAction.one() %}
  {% include '_components/callToAction' with {
      entry: relatedEntry,
      items: relatedEntry.items.all()
  } %}
  </code></pre>
  
  <p>You could as well check if your variable is an object of type <code>EntryQuery</code></p>
  
  <pre><code>{% if className(entry) == 'craft\elements\db\EntryQuery' %}
      {% set entry = entry.one() %}
  {% endif %}
  </code></pre>
  
  <p>Note: since you had multiple of these deprecated errors I wasn't exactly sure which one you wanted to "solve" and which one is "intended"</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-06T12:03:15.187Z
Answers.LastActivityDate: 2018-06-06T12:10:54.390Z
Answers.LastEditDate: 2018-06-06T12:10:54.390Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have a component that accepts both hardcoded objects and entry queries. This allows us to have components that can be used independent of the database.</p>
  
  <p><strong>Hardcode include:</strong></p>
  
  <pre><code>{% include '_components/callToAction' with {
      headline: 'Hardcode call to CTA Headline',
      body: 'Hardcode call to CTA',
      items: [
        { name: 'item1'},
        { name: 'item2'} 
      ]
  } %}
  </code></pre>
  
  <p><strong>db include:</strong></p>
  
  <pre><code>{% include '_components/callToAction' with {
      entry: entry.callToAction
  } %}
  </code></pre>
  
  <p><strong>Component:</strong></p>
  
  <pre><code>{% set component = {
    headline: headline ?? entry.headline ?? null,
    body: body ?? entry.body ?? null,
    items: items ?? entry.items ?? null
  } %}
  
  &lt;h1&gt;{{ component.headline }}&lt;/h1&gt;
  &lt;p&gt;{{ component.body }}&lt;/p&gt;
  
  {% for item in component.items %}
    {{ item.name }}
  {% endfor %}
  </code></pre>
  
  <p>Both those work fine, but with Craft 3 we're getting deprecation errors for not using <code>.all()</code> when it is an element query which is drastically increasing our db queries on the pages that use several of these components.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-05T14:04:09.427Z
Comments.Text: And why do you refuse to execute the query? Seems no difference to me. Of course it makes sense in certain scenarios, but not in yours
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-05T14:52:02.090Z
Comments.Text: Not sure I understand, but `items` in the component won't work on the hardcoded include with .all() but it throws an error without it when it is run with the db include.
Comments.UserDisplayName: Jeremy A
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-05T15:23:55.407Z
Comments.Text: You can either check if the current instance of  `items` is an element of type ElementQuery or you pass the related items within it. `entry: entry.callToAction, items: entry.items.all()` or `items: items ?? entry.items.all() ?? null` I just want to understand why you don't want to execute it
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-06T11:50:05.677Z
Comments.Text: @RobinSchambach - I do want to execute it, I updated my example code which I think is where the confusion is coming from.
Comments.UserDisplayName: Jeremy A
Tags: <craft3>
Title: For loop that can accept array and element query without throwing .all deprecation error
ThreadText: # For loop that can accept array and element query without throwing .all deprecation error
  
  (Asked by: username_0 on 2018-06-05)
  
  I have a component that accepts both hardcoded objects and entry queries. This allows us to have components that can be used independent of the database.
  
  **Hardcode include:**
  
  ```
  {% include '_components/callToAction' with {
      headline: 'Hardcode call to CTA Headline',
      body: 'Hardcode call to CTA',
      items: [
        { name: 'item1'},
        { name: 'item2'} 
      ]
  } %}
  
  ```
  
  **db include:**
  
  ```
  {% include '_components/callToAction' with {
      entry: entry.callToAction
  } %}
  
  ```
  
  **Component:**
  
  ```
  {% set component = {
    headline: headline ?? entry.headline ?? null,
    body: body ?? entry.body ?? null,
    items: items ?? entry.items ?? null
  } %}
  
  <h1>{{ component.headline }}</h1>
  <p>{{ component.body }}</p>
  
  {% for item in component.items %}
    {{ item.name }}
  {% endfor %}
  
  ```
  
  Both those work fine, but with Craft 3 we're getting deprecation errors for not using `.all()` when it is an element query which is drastically increasing our db queries on the pages that use several of these components.
  
  **Tags:** craft3
  
  > **Comments:**
  > **username_1**: And why do you refuse to execute the query? Seems no difference to me. Of course it makes sense in certain scenarios, but not in yours  
  > **username_0**: Not sure I understand, but `items` in the component won't work on the hardcoded include with .all() but it throws an error without it when it is run with the db include.  
  > **username_1**: You can either check if the current instance of  `items` is an element of type ElementQuery or you pass the related items within it. `entry: entry.callToAction, items: entry.items.all()` or `items: items ?? entry.items.all() ?? null` I just want to understand why you don't want to execute it  
  > **username_0**: @username_1 - I do want to execute it, I updated my example code which I think is where the confusion is coming from.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-06-06. Score: 1
  
  Instead of trying to loop through an object (your Query) you could just execute it
  
  ```
  {% set component = {
      headline: headline ?? entry.headline ?? null,
      body: body ?? entry.body ?? null,
      items: items ?? entry.items.all() ?? null
  } %}
  
  <h1>{{ component.headline }}</h1>
  <p>{{ component.body }}</p>
  
  {% for item in component.items %}
      {{ item.name }}
  {% endfor %}
  
  ```
  
  or if that doesn't fit your case, you can do it before
  
  ```
  {% set relatedEntry = entry.callToAction.one() %}
  {% include '_components/callToAction' with {
      entry: relatedEntry,
      items: relatedEntry.items.all()
  } %}
  
  ```
  
  You could as well check if your variable is an object of type `EntryQuery`
  
  ```
  {% if className(entry) == 'craft\elements\db\EntryQuery' %}
      {% set entry = entry.one() %}
  {% endif %}
  
  ```
  
  Note: since you had multiple of these deprecated errors I wasn't exactly sure which one you wanted to "solve" and which one is "intended"
  
  
  ---
  
  