#How do I get a relation document to show up in a collection?

3 messages · Page 1 of 1 (latest)

chilly onyx
#

I have a collection Properties that hasMany Reviews.

Each Review hasOne Property

How can I get the reviews to show on the Property collection page?

// Properties
//...
fields: [
//...
  {
    name: 'reviews',
    type: 'relationship',
    relationTo: 'ratings',
    hasMany: true,
  }
]
//...

And my Ratings collection:

import { CollectionConfig } from 'payload/types'

const Ratings: CollectionConfig = {
  slug: 'ratings',
  access: {
    read: () => true,
  },
  admin: {
    useAsTitle: 'name',
  },
  fields: [
    {
      name: 'name',
      label: 'Customer Name',
      type: 'text',
    },
    {
      name: 'email',
      type: 'text',
    },
    {
      name: 'rating',
      type: 'number',
    },
    {
      name: 'review',
      type: 'textarea',
    },
    {
      name: 'property',
      type: 'relationship',
      relationTo: 'properties',
      hasMany: false,

    }
  ],
}

export default Ratings

I think I need to use filterOptions but I'm not sure how to write a query for that. Also, I don't want the user to select the reviews individually on the Property collection page. I want them to show up there when a new review is created and associated to the Property

Any help would be greatly appreciated

Sincerely,
Lost Frontend Dev

serene bridgeBOT
gleaming patrol
#

Hey @chilly onyx I might have encountered something similar that might get you at least pointed in a direction. I did this a while ago and the exact reasons why I did everything I'm attempting to recall also just FYI this is v2 feedback.

It looks like your collection configs make sense.

But on your Properties reviews realationship field you are going to use some magic with hooks to essentially make a computed field (a field that shows the data you want). One of the contributors or maintainers can butt in to add as I'm just a payload noob over here but just to give you context on my situation and my code and how it can compare. I have employees that can have mulitple assets and an asset that can only have one employee (similar to your Properties and Reviews).

My assets collection relationship field is configured just like your Reviews collection.

My Employee collection realtionship field (where I want to show all employees assets) uses two hooks beforeChange and afterRead

Hopefully this might at least spark some ideas.