Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Query a Model based on its Relation in Strapi

Hope you are all having a nice day! :) I have tried to google my question but never can get a working example.

I have a model for Projects. Projects has a 1:n relationship with the model Members. If I use:

strapi.query('project').find({id: 1});

I get:

{ id: 1, name: 'Project 1', members: [...]}

So basically I am able to find a Project by its ID and I get a list of its Members.

BUT how would I go about getting a list of all members that belong to Project with ID = 1?

strapi.query('member').find(...);
strapi.query('member').search(...);

The closest I got was using:

strapi.query('member').search({_q: 'Project Name'});

This is however not close enough. It is more of a work around. I would really like to be able to do this in a clean way by using the IDs of the Project. Can anyone help or point me in the right direction? Any examples are greatly appreciated! Thank you! :)

like image 297
Philipp Panik Avatar asked Jul 01 '20 15:07

Philipp Panik


People also ask

How do you use a Strapi query?

You can just call strapi. query('modelName', 'pluginName') to access the query API for any model. These queries handle for you specific Strapi features like components , dynamic zones , filters and search .

What is relation in Strapi?

Relations can be set in Strapi's Collection types, Single types, and Components. The relation is set when adding fields to our Collection, Single collection, or Component type.

How do you populate a Strapi?

Complex populating can be achieved by using the filters parameter and select or populate nested relations or components: const entries = await strapi. entityService. findMany('api::article.

How do you make a Strapi model?

Use the CLI, and run the following command strapi generate:model user firstname:string lastname:string . Read the CLI documentation for more information. This will create two files located at ./api/user/models : User.

What is the strapi Query Engine API?

Strapi provides a Query Engine API to interact with the database layer at a lower level. It should mostly be used by plugin developers and developers adding custom business logic to their applications. Strapi's Query Engine API gives unrestricted internal access to the database layer.

What is a relationship in strapi?

This relationship is like a pointer or reference. They point to data in a table that depicts what they contain. There are types of relationships we can establish in Strapi: One-to-One (1:1) In this one-to-one relationship, a column in a table points to only one column in another table.

Are database transactions available in strapi?

Because database transactions are an undocumented feature within Strapi, we need to look at the query function declarations in the source code.

What are strapi attribute types?

Each attribute has a type parameter, which describes its nature and defines the attribute as a simple piece of data or a more complex structure used by Strapi. scalar types (e.g. strings, dates, numbers, booleans, etc.), component to define a component (i.e. a data structure usable in multiple content-types)


2 Answers

The best way to do this is:

strapi.query('member').find({name:'','project.id':id});

Above is AND find

strapi.query('member').find({name:''},['project.id',id]);

Above is OR find

like image 139
ziggybaba Avatar answered Oct 27 '22 23:10

ziggybaba


You can add a custom controller function in project/controller/project.js

getMembers: async ctx => {
  const {id} = ctx.params; 
  const project = await strapi.services.project.findOne({id});
  
  return project ? project.members : [];
}

Make sure that you expose the route. Add the following in project/config/routes.json

{
  "method": "GET",
  "path": "/projects/get-members/:id",
  "handler": "project.getMembers",
  "config": {
    "policies": []
  }
},

You can also change the "path" property to whatever suits your needs.

like image 42
Svetoslav Petrov Avatar answered Oct 27 '22 22:10

Svetoslav Petrov