Laravel Warrant Compiles Row-Level Permissions Into Eloquent SQL
Open-source package Laravel Warrant compiles row-level authorization policies into Eloquent SQL queries and returns per-row UI ability maps.
Maintaining consistent row-level authorization is one of the most persistent sources of subtle bugs in web applications. Developers routinely write ownership or status checks inside Laravel policy methods, then duplicate the exact same logic inside Eloquent query scopes for index screens. Laravel Warrant, an open-source package by Patrick Hanna highlighted by Laravel News, eliminates that drift by compiling authorization rules directly into SQL conditions.
Ending Policy-and-Query Drift in Eloquent
Laravel's native gates and policies excel at expressing arbitrary PHP rules for a single loaded model, yet the framework cannot automatically translate PHP policy methods into database WHERE clauses. As a result, when business logic changes — for example, employees can view their own timesheets and edit them only until a record is locked — updating the policy while forgetting the list query immediately causes UI lists and backend endpoints to disagree.
Warrant tackles this by defining access conditions once as Query Builder constraints inside a dedicated ModelSchema class, then combining them with a concise declarative rule language:
- Condition methods: Annotated with
#[RowCondition], methods such asisOwnerorisLockedreceive aRowConditionContextand apply standard Eloquent query clauses using$c->column()so column references remain safe even under aliased queries. - Declarative rules: Method names map to snake_case identifiers in rules like
if is_owner they can view, updateandif is_locked they cannot update. - Strict precedence: Explicit denials override grants, and any declared ability without a matching grant defaults to denied.
Currently in beta (supporting Laravel 11, 12, and 13 on PHP 8.2+, with PHP 8.3+ required by Laravel 13), the maintainer advises pinning an exact version in Composer (such as patrickhanna/laravel-warrant:4.12.3) and reviewing changelogs before upgrading. Teams that prefer storing dynamic rules outside PHP classes can supply an IRuleProvider to load rules from a database or .warrant files.
Pre-Pagination Filtering and Per-Row UI Abilities
Filtering records in PHP after fetching a database page breaks pagination: a page configured for 20 records can shrink unpredictably, while the total count still exposes inaccessible rows. Once a model attaches the HasWarrantSchema trait and registers in config/warrant.php, Warrant pushes access checks straight into the SQL query before pagination executes:
- Calling
Timesheet::query()->userHasAbility('update')->orderByDesc('id')->paginate(20)filters unauthorized rows at the database layer. - Passing a user instance as the second argument lets services query on another user's behalf.
Equally useful for modern frontends is per-row ability projection. By chaining ->selectUserAbilities(onlyAbilities: ['update']) and casting the model's abilities attribute to array, Eloquent returns a decoded list such as ['update'] or [] alongside every row. Supported on PostgreSQL, MySQL/MariaDB, and SQLite, this lets controllers and APIs tell the UI whether to render an Edit button without running N+1 policy checks in PHP loops.
Seamless Gate Integration and Ecosystem Comparison
Hiding an interface button never replaces endpoint protection. Warrant hooks into Laravel via Gate::before by default, meaning standard $request->user()->can('update', $timesheet), Gate::authorize('update', $timesheet), Blade directives, and can: route middleware automatically evaluate Warrant schemas. When a schema declares an ability, Warrant takes ownership and Laravel skips the corresponding policy method; undeclared abilities continue through standard Laravel authorization.
In the broader Laravel ecosystem, Warrant occupies a distinct niche rather than replacing every permission tool:
- Native Laravel policies remain ideal when a handful of PHP methods cover all access rules without complex list filtering.
- Spatie Laravel Permission focuses on assigning roles and permissions to users, and can be paired with Warrant by writing a rule provider that maps Spatie roles into Warrant rules.
- Bouncer supports model-level grants and ownership, but does not automatically compile row-state rules into filtered Eloquent pagination queries and per-row ability maps.
Performance Considerations Before Adopting
Translating authorization into SQL shifts execution cost from PHP loops to the database planner. As Warrant's documentation notes, requesting per-row abilities adds a SQL predicate for each ability selected. Engineers should inspect generated SQL against production-like datasets — especially when row conditions join or query related tables — and pass onlyAbilities to fetch strictly the flags a screen needs. For data-heavy SaaS apps tired of synchronizing policies and query scopes by hand, Warrant offers a clean single source of truth.
// comments (0)