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
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-optionsYeah, 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,
};
},
},
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 },
};
},
Thanks that solved it
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.