Using laravel policy to filter eloquent query

Posted 1 month ago by DavidPetrov

So I recently just got into the concept of gates and policies and totally loved it. However, a natural question that appeared alongside my work was if I could somehow simply use similar logic to filter my eloquent queries based on access constraints defined in policies, because that would really optimise my infrastructure and boost performance.

I can, however, see that policies are made to work with implicit model binding and are using fetched instances. Would there be an analogy for such an approach when building queries anyway?

A modest example would be the following: I've got two models, Role and DocumentCategory that share a belongsToMany relationship. Every User has a role and I want to only fetch the documents for the use to see, whose categories belong to the user's role. Policies work like a charm and I realize filtering my query would not be that simple.

Any Ideas? Thanks in advance!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.