<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>

CC BY-SA 4.0
2019-02-11T09:46:26.237Z
Thank you so much carlcs! Works like magic and I now understand the concepts!
DorinV
CC BY-SA 4.0
2019-02-11T13:37:33.390Z
I’m glad it helped @DorinVancea, the docs on element queries are very good in my opinion.
carlcs
CC BY-SA 4.0
2019-02-11T09:05:26.403Z
2019-02-11T09:16:35.590Z
2019-02-11T09:16:35.590Z
carlcs
<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>

<entries><relations><channels>
Relationship between channels/entries
# 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? :( #}
    
  {% 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, 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() %}
            
        {% endfor %}

        {{ plantEntity.title }}
    {% endfor %}
{% endfor %}

You should generally avoid querying in a loop though, and use eager-loading 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 %}
            
        {% endfor %}

        {{ plantEntity.title }}
    {% endfor %}
{% endfor %}

If you’ve got many plantCare entries consider paginating 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 @username_0ancea, the docs on element queries are very good in my opinion.  

---

