Answers.Body: <p>It’s important to understand that using relations fields in the template works similar to using <code>craft.entries</code>. They return a <a href="https://docs.craftcms.com/v3/dev/element-queries/" rel="nofollow noreferrer">element query object</a>, for example <code>entry.plantEntity</code> in your entries loop. And you need to call the <code>all()</code> method on them to query for the elements. The same applies to querying for the asset elements in your inner entries loop.</p>
  
  <pre><code>{% set plantCares = craft.entries
      .section('plantCare')
      .all() %}
  
  {% for plantCare in plantCares %}
      {% for plantEntity in plantCare.plantEntity.all() %}
          {% for plantPhoto in plantEntity.plantPhoto.all() %}
              &lt;img src="{{ plantPhoto.getUrl() }}" alt="{{ plantPhoto.title }}"&gt;
          {% endfor %}
  
          {{ plantEntity.title }}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>You should generally avoid querying in a loop though, and use <a href="https://docs.craftcms.com/v3/dev/eager-loading-elements.html" rel="nofollow noreferrer">eager-loading</a> instead.</p>
  
  <pre><code>{% set plantCares = craft.entries
      .section('plantCare')
      .with([
          'plantEntity',
          'plantEntity.plantPhoto'
      ])
      .all() %}
  
  {% for plantCare in plantCares %}
      {% for plantEntity in plantCare.plantEntity %}
          {% for plantPhoto in plantEntity.plantPhoto %}
              &lt;img src="{{ plantPhoto.getUrl() }}" alt="{{ plantPhoto.title }}"&gt;
          {% endfor %}
  
          {{ plantEntity.title }}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>If you’ve got many <code>plantCare</code> entries consider <a href="https://docs.craftcms.com/v3/dev/tags/paginate.html" rel="nofollow noreferrer">paginating</a> through them.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-11T09:46:26.237Z
Answers.Text: Thank you so much carlcs! Works like magic and I now understand the concepts!
Answers.UserDisplayName: DorinV
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-11T13:37:33.390Z
Answers.Text: I’m glad it helped @DorinVancea, the docs on element queries are very good in my opinion.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-11T09:05:26.403Z
Answers.LastActivityDate: 2019-02-11T09:16:35.590Z
Answers.LastEditDate: 2019-02-11T09:16:35.590Z
Answers.UserDisplayName: carlcs
Body: <p>I need some help. Really struggle understanding how to build relationships between channels/entries.</p>
  
  <p>Context/Want:
  I have two separate channels:
  1. "Plant catalogue"
  2. "Plant care"
  I want to re-use the entries from "Plant catalogue" across the whole website wherever I need it accesing speicifc fields.</p>
  
  <p>What I have:
  "Plant care" has an entries field type to help build the relationship with "Plant catalogue": field name: "plantEntity"
  "Plant catalogue" has an asset field that include a photo of the plant. fiel name: "plantPhoto"</p>
  
  <p>My code so far:</p>
  
  <pre><code>{% set plantcareListing = craft.entries().section('plantCare').limit(null)%}
  {% set entries = plantcareListing.all() %}
  
  {% for entry in entries %}
    {% for image in entry.plantEntity %} {# how do I access the asset field "plantPhoto" here? :( #}
      &lt;img src="{{ image.getUrl() }}" alt="{{ image.title }}"&gt;
    {% endfor %}
  
    {{ entry.title }}
  {% endfor %}
  </code></pre>
  
  <p>I would appreciate your advice. Thank you!</p>
  
Tags: <entries><relations><channels>
Title: Relationship between channels/entries
ThreadText: # Relationship between channels/entries
  
  (Asked by: username_0 on 2019-02-11)
  
  I need some help. Really struggle understanding how to build relationships between channels/entries.
  
  Context/Want:
  I have two separate channels:
  1. "Plant catalogue"
  2. "Plant care"
  I want to re-use the entries from "Plant catalogue" across the whole website wherever I need it accesing speicifc fields.
  
  What I have:
  "Plant care" has an entries field type to help build the relationship with "Plant catalogue": field name: "plantEntity"
  "Plant catalogue" has an asset field that include a photo of the plant. fiel name: "plantPhoto"
  
  My code so far:
  
  ```
  {% set plantcareListing = craft.entries().section('plantCare').limit(null)%}
  {% set entries = plantcareListing.all() %}
  
  {% for entry in entries %}
    {% for image in entry.plantEntity %} {# how do I access the asset field "plantPhoto" here? :( #}
      <img src="{{ image.getUrl() }}" alt="{{ image.title }}">
    {% endfor %}
  
    {{ entry.title }}
  {% endfor %}
  
  ```
  
  I would appreciate your advice. Thank you!
  
  **Tags:** entries, relations, channels
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-11. Score: 2 | Accepted answer
  
  It’s important to understand that using relations fields in the template works similar to using `craft.entries`. They return a [element query object](https://docs.craftcms.com/v3/dev/element-queries/), for example `entry.plantEntity` in your entries loop. And you need to call the `all()` method on them to query for the elements. The same applies to querying for the asset elements in your inner entries loop.
  
  ```
  {% set plantCares = craft.entries
      .section('plantCare')
      .all() %}
  
  {% for plantCare in plantCares %}
      {% for plantEntity in plantCare.plantEntity.all() %}
          {% for plantPhoto in plantEntity.plantPhoto.all() %}
              <img src="{{ plantPhoto.getUrl() }}" alt="{{ plantPhoto.title }}">
          {% endfor %}
  
          {{ plantEntity.title }}
      {% endfor %}
  {% endfor %}
  
  ```
  
  You should generally avoid querying in a loop though, and use [eager-loading](https://docs.craftcms.com/v3/dev/eager-loading-elements.html) instead.
  
  ```
  {% set plantCares = craft.entries
      .section('plantCare')
      .with([
          'plantEntity',
          'plantEntity.plantPhoto'
      ])
      .all() %}
  
  {% for plantCare in plantCares %}
      {% for plantEntity in plantCare.plantEntity %}
          {% for plantPhoto in plantEntity.plantPhoto %}
              <img src="{{ plantPhoto.getUrl() }}" alt="{{ plantPhoto.title }}">
          {% endfor %}
  
          {{ plantEntity.title }}
      {% endfor %}
  {% endfor %}
  
  ```
  
  If you’ve got many `plantCare` entries consider [paginating](https://docs.craftcms.com/v3/dev/tags/paginate.html) through them.
  
  > **Comments:**
  > **username_0**: Thank you so much username_1! Works like magic and I now understand the concepts!  
  > **username_1**: I’m glad it helped @DorinVancea, the docs on element queries are very good in my opinion.  
  
  ---
  
  