Define reverse relationship with Tailor

Hello,

When you use tailor to link one model (let’s imagine Blog\Post and Blog\Category), It’s quite easy to do, in my post blueprint, I have

categories:
    label: Catégories
    type: entries
    source: Blog\Category

so, using twig, I can easily access all categories associated to a post using {{ post.categories }}

But it seems that the reverse relation is not made automatically, so my question is, how can I access all posts associated to a categories ?

Tested this :

{% for category in collection %}
   {{ d(category.posts) }} // return null, I would like it to return the collection of all posts associated to that category.
{% endfor %}

Thank you

2 Likes

We should be able to add support for this by defining the inverse relationship and optionally hiding the field. For example, something like this:

# Coming soon...
posts:
    type: entries
    # ...
    inverse: true # Inverse relation
    hidden: true # Hide form field (optional)

I have added this to the internal tracker for review. Thanks!

Hello,

Ahh, it would be a really nice addition. Thank you for your support.

Best regards,

This feature is available in v3.2.12. Documented here:

The syntax is slightly different, the inverse property should define the “other relation”:

posts:
    type: entries
    source: Blog\Post
    inverse: categories
    hidden: true
2 Likes

Excellent news, thank you so much

Hi @daft , is it possible to define the reverse relation withrepeater type?

Hello @abbasadel

Yes the Tailor\Models\RepeaterItem has a host relation defined that is the inverse relation.

1 Like