Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directus - how to show Many2Many relationship in api

Tags:

directus

I am trying to make a blog in Directus. I created Blog Collection and Categories Collection. I am using junction table with many to many relationship to connect categories to blog. I can create a new blog post and select category from category collection and I can add a new category also. But when I am calling in api, it is not showing the category field in my rest api call. I can call category endpoint and view the list of all the categories in rest api but in blog collection endpoint, I cant view related category name.

I want to view all the selected category name to each blog posts in my rest api. And I want to view all the blog posts when I call category collection in my rest api by putting category name like projectname/items/categories/aliens/ so I can see all the blog posts of aliens categories.

Currently I am getting this by calling my blog collection in json -

{
  "data": [
    {
      "id": 1,
      "status": "published",
      "owner": 1,
      "created_on": "2020-03-16 21:15:25",
      "blog_title": "first blog post title",
      "blog_content": "<p>sdlkjfhsdkfol8ysdmfhj sd,fh klsdhf msoiudf oihsdyfghm soreht4,7shetc,oiuh,scenclkr</p>\n<p><strong>dsfgdsfgds</strong></p>"
    }
  ]
}

How to get the category name on rest api ?

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

like image 580
Mayanktaker Avatar asked Sep 17 '25 15:09

Mayanktaker


1 Answers

Did you try using the fields parameter? This is used to fetch deeply nested relational data:

?fields=*.*.*

In this example, the asterisk is a wildcard for_all_ fields at that relational depth... so this fetches three levels deep.

https://docs.directus.io/api/query/fields.html

like image 51
RANGER Avatar answered Sep 19 '25 06:09

RANGER



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!