Like what we’re doing? Star us on GitHub!

FilterOptions for Array contains Array

Dave Damage
last month
5

Hi,



I have the following Issue:



I have a category collection and a module collection, both of these have a technologies field (relationship field with hasMany option for a different collection). In my module field, I want to have a category field which automatically filters the available categories by the technologies of the module.



My main idea is to write a custom function that maps over the technologies of the module and checks if the technology is in the array of the categories' technology field and combine it with the OR clause.



I am honestly not sure if that is a viable solution and maybe there is something simpler, so if anybody has an idea it would be cool to hear it. Thanks in advance

  • Jarrod
    Payload Team
    last month

    Have you seen the filterOptions property on relationship fields? It might be just what you are looking for

    https://payloadcms.com/docs/fields/relationship#filtering-relationship-options
  • Dave Damage
    last month

    Yeah, that's what I am using. Sorry for not mentioning that



        {
          name: 'category',
          label: 'Themenblock',
          type: 'relationship',
          required: true,
          relationTo: 'categories',
          filterOptions: ({ data }) => {
            const query = data?.technologies?.map((id) => ({
              technologies: {
                in: id,
              },
            }));
            return {
              or: query,
            };
          },
        },
  • jesschow
    Payload Team
    2 weeks ago

    hi @Dave Damage - I think you're on the right path here. I would expect the filterOptions query to look something like this:



          filterOptions: ({ data }) => {
            return {
              technologies: { in: data.technologies },
            };
          },
  • Dave Damage
    2 weeks ago

    Thanks that solved it

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More