axiosstrapipopulatetable-relationships

I need to populate the axios get response with two leveles depth with Strapi v4


I have a database schema with 2 levels relationships between tables:

enter image description here

I perform this axios get: http://localhost:1337/api/restaurants?filters[id][$eq]=2&populate=* to strapi

And I hope to get two levels of data, but instead I get one level:

{

"data": [

    {

        "id": 2,

        "attributes": {

            "name": "Best Pizza",

            "address": "some address",

            "phone": "some phone",

            "createdAt": "2022-10-28T11:29:48.764Z",

            "updatedAt": "2022-11-03T23:33:35.118Z",

            "publishedAt": "2022-10-28T11:29:51.410Z",

            "logo_image_url": "some image url

            "selected_theme": 1,

            "facade_image_url": "https://i.ibb.co/HpzRt06/best-pizza-facade.jpg",

            "website_url": "https://bestpizza.com",

            "categories": {

                "data": [

                    {

                        "id": 1,

                        "attributes": {

                            "name": "Pizzas",

                            "image_url": "an image url",

                            "createdAt": "2022-10-28T16:43:36.622Z",

                            "updatedAt": "2022-10-28T16:58:34.786Z",

                            "publishedAt": "2022-10-28T16:55:00.417Z",

                            "description": "This are our delicious pizzas"

                        }

                    },

                    {

                        "id": 3,

                        "attributes": {

                            "name": "Drinks",

                            "image_url": "some image url",

                            "createdAt": "2022-10-29T00:31:37.785Z",

                            "updatedAt": "2022-11-01T19:58:37.738Z",

                            "publishedAt": "2022-10-29T00:31:37.778Z",

                            "description": "These are our delicious drinks"

                        }

                    },

                    {

                        "id": 4,

                        "attributes": {

                            "name": "Desserts",

                            "image_url": "some image url",

                            "createdAt": "2022-11-01T20:08:15.642Z",

                            "updatedAt": "2022-11-01T20:08:15.642Z",

                            "publishedAt": "2022-11-01T20:08:15.638Z",

                            "description": "These are our delicious desserts"

                        }

                    }

                ]

            }

        }

    }

],

"meta": {

    "pagination": {

        "page": 1,

        "pageSize": 25,

        "pageCount": 1,

        "total": 1

    }

}

}

As you can see, there are no dishes as I expected for having populate=* in the query string, I expect to have the dishes for each category too

What am I missing here to have all levels in the same query in strapi


Solution

  • In v4, this is currently not possible in the way you planned to do it, unless you use a plugin or be more specific with the population.

    Populate Deep plugin

    In the strapi population docs, you can find how to be more specific with populations and get relations 2 and more levels deep.

    const qs = require('qs');
    const query = qs.stringify({
      populate: {
        author: {
          populate: ['company'],
        }
      }
    }, {
      encodeValuesOnly: true, // prettify URL
    });