logo light

Implementing Row-Level Security in Filament Tables with applyRowAccessPolicy

Image Description
By: André Domingues
  • 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.

Implementing Row-Level Security in Filament Tables with applyRowAccessPolicy

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

  1. The macro accepts an optional condition parameter that determines whether the policy should be applied

  2. It stores this condition in Laravel's service container with a unique identifier for each table instance

  3. It modifies the query by checking if a policy exists for the model and if it contains an applyRowAccessPolicy method

  4. If found, it calls this method, passing the current user and the query builder

  5. 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.

Back to Tricks