Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Need help around the relationship field.

default discord avatar
sumanta111last year
21

Hi!



I am building this admin where in one collection, I have an address field which relates to another collection. How do I restrict the admin UI to stop listing all the available addresses and only show the selected one, if not selected any then it should only show the '+' button



What is happening right now: [Attached]



Collections:


Vendors.ts:


||import { CollectionConfig } from "payload/types"; const Vendors: CollectionConfig = { slug: "vendors", admin: { useAsTitle: "vendorName", }, fields: [ { name: "vendorName", type: "text", required: true, }, { name: "vendorCode", type: "text", admin: { readOnly: true, }, required: false, }, { name: "contactPerson", type: "relationship", relationTo: "contact-person", required: true, hasMany: false, admin: { disableListColumn: true, }, }, { name: "bankAccount", type: "relationship", relationTo: "bank-accounts", required: true, hasMany: false, admin: { disableListColumn: true, }, }, { name: "address", type: "relationship", relationTo: "addresses", required: true, hasMany: false, filterOptions: ({ relationTo, siblingData }) => { if (relationTo === "addresses") { return { and: [], }; } }, admin: { disableListColumn: true, disableListFilter: true, isSortable: false, }, }, ], }; export default Vendors; ||

Found the answer. Anybody needs help on this can ping me

  • default discord avatar
    poofy25last year

    Hey i think i have a similar issue,


    I have a categories collection and a product collection


    Each product has a relationship with a category


    And a category has relationships with the products


    How do i make it so when choosing the relationship in the category collection that only products with that category relationship show up?

  • default discord avatar
    sumanta111last year

    I made a similar design with categories and sub categories so that only sub categories from that category show up in the dropdown. Here is the code:




    import { CollectionConfig } from "payload/types"; const Categories: CollectionConfig = { slug: "categories", admin: { useAsTitle: "name", description: "Categories of products", }, fields: [ { name: "name", type: "text", required: true, }, { name: "description", type: "text", required: false, }, { name: "image", type: "upload", relationTo: "media", }, { name: "subCategory", type: "relationship", relationTo: "sub-categories", required: true, hasMany: true, filterOptions: ({ relationTo, data, siblingData }) => { console.log(relationTo); // Type of realtion (The slug for the child collection) console.log(data); // The actual collection console.log(siblingData); // Whatever being edited on screen if (relationTo === "sub-categories") { if (siblingData["subCategory"]) { // This applies to when you are editing the collection return { _id: { in: siblingData["subCategory"], // This is the key. It follows mongo like query syntax. You can read more here: https://payloadcms.com/docs/queries/overview }, }; return true; } else { // This applies to when you are creating the collection for the first time. return false; } } }, }, ], }; export default Categories;
  • default discord avatar
    poofy25last year

    any idea about this ts error ?

  • default discord avatar
    sumanta111last year

    Don't use '.' operator. use siblingData['products']

  • default discord avatar
    poofy25last year

    still get the same error

  • default discord avatar
    sumanta111last year

    Did you fix this? Can you paste the complete code?

  • default discord avatar
    poofy25last year

    Sadly no i still get the same error

  • default discord avatar
    sumanta111last year

    Share the code, I'll take a look.

  • default discord avatar
    poofy25last year

    I dont have my laptop rn, but its the exact same one u send me but i just modified the collection

  • default discord avatar
    sumanta111last year

    Ok. You can share the code once you get some time. I spent hours on this trying to resolve.

  • default discord avatar
    poofy25last year
    import { CollectionConfig } from 'payload'
    
    const Categories: CollectionConfig = {
      slug: 'categories',
      admin: {
        useAsTitle: 'name',
        group:'Ecommerce'
      },
      access: {
        read: () => true,
      },
      fields: [
        {
          name: 'image',
          type: 'upload',
          label: 'Category Image',
          relationTo:'media',
          required: true,
        },
        {
          name: 'name',
          type: 'text',
          label: 'Name',
          required: true,
          localized: true,
        },
        {
          name: 'description',
          type: 'text',
          label: 'Description',
          localized: true,
        },
        {
          name: 'products',
          type: 'relationship',
          relationTo: ['products','splitPizza'],
          hasMany: true,
          filterOptions: ({ relationTo, data, siblingData }) => {
            console.log(relationTo); // Type of realtion (The slug for the child collection)
            console.log(data); // The actual collection
            console.log(siblingData); // Whatever being edited on screen
            if (relationTo === "products") {
              if (siblingData["products"]) { // This applies to when you are editing the collection
                return {
                  _id: {
                    in: siblingData["products"],  // This is the key. It follows mongo like query syntax. You can read more here: https://payloadcms.com/docs/queries/overview
                  },
                };
                return true;
              } else { // This applies to when you are creating the collection for the first time. 
                return false;
              }
            }
            return false
    
        },
      ],
    }
    
    export default Categories


    here is the colleciton code



  • default discord avatar
    vpjmlast year
  • default discord avatar
    poofy25last year

    yeah ?

  • default discord avatar
    vpjmlast year

    so its 'normal' that typescript doesn t like it



    you can cast it to whatever type you need

  • default discord avatar
    poofy25last year

    so i just let it be ?

  • default discord avatar
    vpjmlast year

    i think so

  • default discord avatar
    poofy25last year

    but it still dosent work

  • default discord avatar
    sumanta111last year

    Does it run or shows error in console?

  • default discord avatar
    poofy25last year

    yeah

  • default discord avatar
    sumanta111last year

    Code seems ok to me

  • default discord avatar
    vpjmlast year

    Isn't it just a TypeScript error? Normally, the code should be able to execute

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.