Ensuring exclusive role filters with Spatie in Laravel
Hello everyone,
I’m encountering an issue with my role-based filters in Laravel (using Spatie for role management). Right now, when I select multiple roles—such as super_admin and doctor—the query returns any record that has either role. In other words, I see:
- Users with only the super_admin role
- Users with only the doctor role
- Users who have both roles
What I want instead is an exclusive filter. If I select super_admin and doctor, I should only see users who have both of those roles simultaneously.
Below is the relevant code snippet I’m using to apply these filters. Any guidance on how to make the role filter “AND” instead of “OR” would be greatly appreciated:

Solution:Jump to solution
Check the
apply()
method of the SelectFilter
. When using multiple
in uses whereIn()
. You can create or own Filter, extend the SelectFilter
and overwrite the apply
method to your liking.
Lines 166–182...2 Replies
Solution
Check the
apply()
method of the SelectFilter
. When using multiple
in uses whereIn()
. You can create or own Filter, extend the SelectFilter
and overwrite the apply
method to your liking.
Lines 166–182Thank you, it's working fine now.
Here is the exclusive filter in case anyone would need it :