Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Arasma's avatar

Filtering query results based on permissions

Hello, I would like to know what is the best approach to filtering eloquent query results based on user permissions. Let's say I have Orders with many possible statuses including open and ready and many user roles including admin and operator. On the same view I need to display filtered orders based on user role. E.g. admin will see all of them, operator will only see open orders, another role will see a combination of statuses etc.

I am thinking about few options:

  1. Filter it through global scope - somebody said scopes are not suitable for filtering (why?)
  2. Filter it in each controller - but there are many controllers / actions around Orders
  3. Filter it directly in templates in every foreach - sounds weird.
  4. ???

Thanks for any advice.

0 likes
2 replies
Talinon's avatar

Sounds like a good case for creating a repository which you can inject into your controllers. This way the logic is contained in one place and not scattered throughout your application's controllers and templates.

The repository would be responsible for worrying about building the query scopes that interact with the database based upon the authenticated user's permissions.

If you are unfamiliar with the repository pattern, just google for it.. there are plenty of examples.

Please or to participate in this conversation.