<p>When you use the <code>craft.commerce.products</code> tag, it will return a criteria model, but you are treating it as the first result by accessing the title attribute.</p>

<p>Try <code>{{ relatedProduct.first().title }}</code> instead.</p>

CC BY-SA 3.0
2016-09-30T09:38:10.767Z
You should always check if `first()` actually returns something before accessing properties or methods of the element model.
carlcs
CC BY-SA 3.0
2016-09-30T15:57:52.243Z
Thanks so much stephen & @carlcs. Looks like I heed to swat-up on the models
Britchie
CC BY-SA 3.0
2016-09-30T16:43:07.237Z
Also, with the way I have the entries set-up presently, there will only ever be a single relationship returned. Is it possible to add the .first() to the main query instead of for every property? (couldn't find an example)
Britchie
CC BY-SA 3.0
2016-10-03T10:28:39.797Z
@Britchie Exactly as you say, see here: http://craftcms.stackexchange.com/questions/289/how-do-i-target-the-first-item-in-a-twig-for-loop
Stephen Hamilton
CC BY-SA 3.0
2016-09-30T09:18:01.720Z
2016-09-30T09:18:01.720Z
Stephen Hamilton
<p>I'm working with Craft Commerce with a set of individual items in a product type and in another, I have packaged sets of the same. The sets (cardSets) have a relation field to select the required individual cards. The code is in the product entry template for the individual card and my intention is to highlight that the single card can be purchased in a set.</p>

<p>I'm using the following code to do a simple reverse relationship but I can't get this to work and I'm not sure if I've got the syntax correct. I'm not seeing any return or error. How do I know if I'm getting an object back? </p>

<pre><code>{% set relatedProduct = craft.commerce.products({
    type: 'cardSets',
    relatedTo: {
        targetElement: product,
        field: 'productsInSet'
        }
}) %}

{{ relatedProduct.title }}
</code></pre>

<relations><plugin-craftcommerce>
Reverse relationship in Craft Commerce?
# Reverse relationship in Craft Commerce?

(Asked by: username_0 on 2016-09-30)

I'm working with Craft Commerce with a set of individual items in a product type and in another, I have packaged sets of the same. The sets (cardSets) have a relation field to select the required individual cards. The code is in the product entry template for the individual card and my intention is to highlight that the single card can be purchased in a set.
I'm using the following code to do a simple reverse relationship but I can't get this to work and I'm not sure if I've got the syntax correct. I'm not seeing any return or error. How do I know if I'm getting an object back? 
{% set relatedProduct = craft.commerce.products({
    type: 'cardSets',
    relatedTo: {
        targetElement: product,
        field: 'productsInSet'
        }
}) %}

{{ relatedProduct.title }}

**Tags:** relations, plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2016-09-30. Score: 2 | Accepted answer

When you use the `craft.commerce.products` tag, it will return a criteria model, but you are treating it as the first result by accessing the title attribute.

Try `{{ relatedProduct.first().title }}` instead.

> **Comments:**
> **username_2**: You should always check if `first()` actually returns something before accessing properties or methods of the element model.  
> **username_0**: Thanks so much stephen & @username_2. Looks like I heed to swat-up on the models  
> **username_0**: Also, with the way I have the entries set-up presently, there will only ever be a single relationship returned. Is it possible to add the .first() to the main query instead of for every property? (couldn't find an example)  
> **username_1**: @username_0 Exactly as you say, see here: http://craftcms.stackexchange.com/questions/289/how-do-i-target-the-first-item-in-a-twig-for-loop  

---

