Happy Friday. I was wondering if anyone could assist with a filter in one of our collections. We have a Users collection that has fields for Roles, which is a relationship to the Roles collection, and a supervisor field, which is a relationship to Users. What we need to do is add a filter so the supervisor field returns all users that have the "Supervisor" role assigned.
#Filtering Users by Supervisor Role in Collections
32 messages · Page 1 of 1 (latest)
Original message from @rare zenith - Moved from #general message
Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:
Documentation:
Have you looked at using filterOptions? https://payloadcms.com/docs/fields/relationship#filtering-relationship-options
The documentation is confusing.
The user is 'null' when the admin ui is rendering.
Example
doesn't console log anything.
Yes, I've tried the filterOptions as demonstrated in the documentation, however I would need to know the id of the specific role since roles is a relationship field to the Roles collection. That's fine for a single deployment, but we deploy to multiple environments, so the id of that role will be different for each deployment. Thus we can't hard code it.
It does, and that's what we've been using up to this point, however our customer wants us to get away from using Keycloak/jwt tokens since we plan on implementing some very complex RBAC features and we are also planning to integrate with different sources for authentication.
We have to use Payload
by now try to acess the user in filter options
Yes, I can access user in the filter options. Your sample shows all users in the console. Roles is shown as an array. I need to filter for just the users with "Supervisor" in that array.
oic
Actually since the filterOptions is just for user, it lists only the current user.
currently it doesnt provide req for you, you could open a feature request for this
you have two other options right now:
-
a custom component for this so you control exactly which relationships are fetched
-
thinking a bit outside the box, you can provide a custom id to your roles collection, allowing you to keep IDs consistent...if theyre string your ids could be statically set like
admin
any field with its name id will be used as a custom id, the type here will depend on your database configuration as well
@covert chasm im facing a similar problem, can u help me with that please?
https://discord.com/channels/967097582721572934/1235981447211515998
Could you provide an example of how to provide a custom id? That sounds like it might be the way for us to go.
just name a field id with a text type
put it at the top to make it easier
I just found that. Cool I will try it out.
might need a data migration for existing data to be moved over
Didn't have but a few roles, so I was able to clear it and re-add. Here's what I've got for the filterOptions, but not sure yet is this is correct. I'm doing a build now to test it. The custom id for the Supervisor role is 24001. {
name: "supervisor",
type: "relationship",
relationTo: "users",
filterOptions: ({ relationTo, user }) => {
return {
roles: { contains: 24001}
}
}
},
That wasn't correct. Trying filterOptions: ({ relationTo, id })
Nope. Not correct either.
Tried relationTo, siblingData as well
I've tried a number of combinations and can't figure this out.
Finally figured it out. I set the custom id as text instead of number and set the id for Supervisor to 'supervisor'. Then the following filterOptions worked: {
name: "supervisor",
type: "relationship",
relationTo: "users",
filterOptions: () => {
return {
roles: { in: 'supervisor' }
}
}
},
Thanks for share @rare zenith
Bah, forgot how to mark this as solved.