Implementing Row-Level Security in Filament Tables with applyRowAccessPolicy
- Published: 07 May 2025 Updated: 07 May 2025
Learn how to implement row-level security in Filament tables using the applyRowAccessPolicy macro that filters query results based on user permissions through Laravel's policy system.
Understanding the applyRowAccessPolicy Macro
The applyRowAccessPolicy macro provides an elegant solution for implementing row-level security in Filament tables. This macro integrates with Laravel's policy system to filter table data based on user permissions.
When applied to a table, this macro modifies the underlying query by checking for an applyRowAccessPolicy method in your model's Policy class. It then uses this method to filter records the current user is allowed to view.
How It Works
The macro accepts an optional condition parameter that determines whether the policy should be applied
It stores this condition in Laravel's service container with a unique identifier for each table instance
It modifies the query by checking if a policy exists for the model and if it contains an
applyRowAccessPolicymethodIf found, it calls this method, passing the current user and the query builder
The policy method returns a modified query with appropriate row-level filters applied
Implementation Example
Define the method in your model's Policy:
Apply the macro to your Filament table:
This simple implementation ensures users only see rows they're authorized to access, providing robust, reusable row-level security across your Filament application.
Related Tricks:
make all Field or any components translatable
Using an editable column like SelectColumn or ToggleColumn, you can conditionally trigger a modal form when a certain option is selected.
how to use an action as a headerAction or as a table row action without code duplication
Translating components can often be a repetitive task, Fortunately, there's a neat trick to automate this process, making your components instantly translatable.