Search code examples
strapi

Strapi not able to filter relations value


I have two collection. post and common. I have defined common has many Post relation. The relation is called contains.

Post as component called place with country field, which is enumeration with country codes ( two character) So relations will have posts which might have different countries. I trying to filter the relations values. What I want is,the relation field should only have posts with specific country code.

What i have tried

/api/commons?populate[0]=contains.basic&populate[1]=basic&filters[contains][place][country][$eq]=AF&populate[2]=contains.place

But for this the relation will have posts that have different country codes.

Is it possible to filter the relations values? Or is there better way to structure this?


Solution

  • not sure i fully understand your data layout, however it seems that instead filtering the relation of Common you would get the data more easily if you just filter the posts:

    /api/posts?filters[$and][0][common][$eq]=commonId&filters[$and][1][country][$eq]=AF
    

    with query:

        const str = qs.stringify(
            {
                filters: {
                    $and: [{ common: { $eq: 'commonId' } }, { country: { $eq: 'AF' } }],
                },
            },
            { encodeValuesOnly: true }
        );