How to filter on multiple nested relations?

I have a page, which uses a collection named blocks (Many-to-Any). Each of these blocks has multiple translated fields - so another relation called translations.

In my SDK-Api, I want to filter those translations to only return the results in the correct locale.

{
  // page
  id: 2,
  blocks: [
    // blocks collection
    {
      id: 1,
      landing_id: 2,
      collection: "block_hero",
      item: {
        id: 1,
        translations: [
          {
            id: 1,
            block_hero_id: 1,
            languages_code: "de-de",
            title: "German title"
          },
          {
            id: 2,
            block_hero_id: 1,
            languages_code: "en-us",
            title: "English title"
          }
        ]
      }
    }
  ]
}

I already tried this, but it doesn’t work:

{
	deep: {
		blocks: {
			item: {
				translations: {
					_filter: {
						languages_code: {
							_eq: "en-us"
						}
					}
				}
			}
		}
	}
}

All language versions are returned.

How do Filter for page.blocks.item.translations.languages_code === "en-us"?
Thank you!

I'd like to know this too !

Did you find a solution?

For what its worth, I managed to acheive this just fine using graphQL, but couldnt get it to work with directus.request

Did anyone ever figure this out? I am currently also trying to query for translations by languages_code (using a singleton) and can't figure it out.

1 Answer

1

item by itself won’t work here, as blocks.item is a relationship to multiple tables, so there’s no guarantee what fields exist on the other end here. Directus needs the concrete collection scope before it can apply nested deep filters. For the SDK query, scope the nested key as item:block_hero instead of just item and put the translation filter under that:

deep: { 
  blocks: { 
    'item:block_hero': { 
      translations: { 
        _filter: { 
          languages_code: { _eq: 'en-us' }
        } 
      } 
     } 
  } 
}