Laravel Warrant: Row-Level Permissions With Eloquent | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://msaied.com) - [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [About](https://msaied.com#about)

           [  Contact](https://msaied.com#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://msaied.com)
- [ProjectsCase studies](https://msaied.com/projects)
- [ArticlesEngineering notes](https://msaied.com/articles)
- [CertificatesCredentials](https://msaied.com/certificates)
- [AboutHow I work](https://msaied.com#about)
- [ContactGet in touch](https://msaied.com#contact)

  [Start a conversation](https://msaied.com#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://msaied.com)
2. /
3. [Articles](https://msaied.com/articles)
4. /
5. [Laravel](https://msaied.com/articles?category=laravel)
6. /
7. Laravel Warrant: Row-Level Permissions With Eloquent

   [Laravel](https://msaied.com/articles?category=laravel) [Composer Pacakge](https://msaied.com/articles?category=composer-pacakge) 

 Laravel Warrant: Row-Level Permissions With Eloquent
=====================================================

 Laravel Warrant compiles authorization rules into SQL conditions, letting you define ownership and lock checks once and reuse them to filter Eloquent queries, authorize actions, and return per-row ability lists.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://msaied.com#person) Published 18 Nov 2026 · Updated 21 Nov 2026 · 3 min read

ShareCopy linkCopied

 ![Laravel Warrant: Row-Level Permissions With Eloquent](https://cdn.msaied.com/831/01dca3c7fc50828b3a71cb843ab98f72.png) 

  On this page +1. [The Problem: Policies and Query Scopes Drift Apart](#the-problem-policies-and-query-scopes-drift-apart)
2. [How Warrant Compares to Other Authorization Options](#how-warrant-compares-to-other-authorization-options)
3. [Installation](#installation)
4. [Define Conditions in a Schema](#define-conditions-in-a-schema)
5. [Filter Records Before Pagination](#filter-records-before-pagination)
6. [Return Per-Row Abilities](#return-per-row-abilities)
7. [Authorize Actions With Laravel's Gate](#authorize-actions-with-laravels-gate)
8. [Key Takeaways](#key-takeaways)

 The Problem: Policies and Query Scopes Drift Apart
--------------------------------------------------

If you've written an ownership check in a Laravel policy and then repeated it in a query scope, you already know the risk. Change one and forget the other, and your list page can return records that your policy would deny — or hide records it would allow.

[Laravel Warrant](https://github.com/patrickjames242/laravel-warrant), by Patrick Hanna, addresses this by compiling authorization rules into SQL conditions. You define conditions once and use them to filter queries, authorize individual actions, and return per-row ability lists.

How Warrant Compares to Other Authorization Options
---------------------------------------------------

| Option | Best for | |---|---| | Native Laravel policies | Application rules in PHP, queries maintained separately | | Spatie Laravel Permission | Role and permission assignment combined with policy logic | | Bouncer | Roles, abilities, and per-model grants including ownership | | Laravel Warrant | Record-dependent rules that must also filter Eloquent queries and produce per-row ability lists |

Warrant is the right fit when the same access conditions — ownership, lock status, and so on — need to drive both list filtering and action authorization.

Installation
------------

Warrant is in beta. Pin an exact version and review the changelog before upgrading:

```bash
composer require patrickhanna/laravel-warrant:4.12.3
php artisan vendor:publish --tag=warrant-config

```

The package supports Laravel 11, 12, and 13 with PHP 8.2 or later.

Define Conditions in a Schema
-----------------------------

Create `app/Warrant/TimesheetSchema.php` to express ownership and lock rules as query constraints:

```php
namespace App\Warrant;

use App\Models\Timesheet;
use Illuminate\Contracts\Database\Query\Builder;
use Warrant\Rules\RuleProviderContext;
use Warrant\Schema\Abilities\Ability;
use Warrant\Schema\Conditions\Row\RowCondition;
use Warrant\Schema\Conditions\Row\RowConditionContext;
use Warrant\Schema\Types\Classes\ModelSchema;

class TimesheetSchema extends ModelSchema
{
    public const model = Timesheet::class;

    #[Ability] public const VIEW = 'view';
    #[Ability] public const UPDATE = 'update';

    #[RowCondition]
    public function isOwner(RowConditionContext $c): Builder
    {
        return $c->query->where(
            $c->column('user_id'),
            $c->user->getAuthIdentifier(),
        );
    }

    #[RowCondition]
    public function isLocked(RowConditionContext $c): Builder
    {
        return $c->query->where($c->column('locked'), true);
    }

    public function rules(RuleProviderContext $context): string
    {
        return paginate(20);

```

The database filters inaccessible rows before Laravel paginates, so your page counts stay accurate.

Return Per-Row Abilities
------------------------

To decide whether to show an edit button beside each row, select abilities alongside the records:

```php
$timesheets = Timesheet::query()
    ->userHasAbility('view')
    ->selectUserAbilities(onlyAbilities: ['update'])
    ->paginate(20);

foreach ($timesheets as $timesheet) {
    $canEdit = in_array('update', $timesheet->abilities, true);
}

```

Request only the abilities you need. Selecting the full ability list adds a predicate per ability to the query.

Authorize Actions With Laravel's Gate
-------------------------------------

Hiding a button is not enough. Warrant integrates with Laravel's Gate via a `before` hook, so you can use familiar calls:

```php
Gate::authorize('update', $timesheet);

```

The same integration supports Blade directives (`@can`) and the `can:` middleware. Abilities not declared in the schema continue through Laravel's normal authorization handling.

Key Takeaways
-------------

- Warrant compiles PHP-defined conditions into SQL, keeping list filters and authorization rules in sync.
- Denials override grants; a locked record blocks updates even when ownership grants them.
- `userHasAbility` filters at the database level, preserving accurate pagination totals.
- `selectUserAbilities` returns per-row ability arrays, useful for conditional UI elements.
- Gate integration means existing Blade directives and middleware work without changes.
- The package is in beta — pin an exact version in your `composer.json`.

---

Source: [Laravel Warrant: Row-Level Permissions With Eloquent](https://laravel-news.com/laravel-warrant)

- [Laravel](https://msaied.com/articles?search=Laravel)
- [Authorization](https://msaied.com/articles?search=Authorization)
- [Eloquent](https://msaied.com/articles?search=Eloquent)
- [Packages](https://msaied.com/articles?search=Packages)
- [Row-Level Security](https://msaied.com/articles?search=Row-Level%20Security)

 Frequently asked questions 
---------------------------

  How does Laravel Warrant differ from native Laravel policies?Laravel policies let you express authorization rules in PHP, but they do not automatically translate into database query filters. Warrant defines conditions as query constraints, so the same rules can filter Eloquent queries, authorize individual actions, and return per-row ability lists from a single definition.

   Does Laravel Warrant replace Spatie Laravel Permission or Bouncer?Not necessarily. Spatie and Bouncer handle role and permission assignment well. Warrant is most useful when the same record-dependent conditions — such as ownership and lock status — must drive both list filtering and action authorization. You could keep Spatie for role assignments and add a Warrant rule provider that returns rules for those roles.

   Why filter records in the database rather than in a PHP collection?Filtering a collection after fetching a paginated page can leave you with fewer rows than the requested page size and a total count that includes inaccessible records. Applying `userHasAbility` as a query scope lets the database exclude unauthorized rows before Laravel paginates, keeping page sizes and totals accurate.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://msaied.com#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://msaied.com#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleJSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43](https://msaied.com/articles/json-schema-validation-rules-and-a-retryfor-attribute-in-laravel-1343) [Next articlePHP 8.6 Released: Partial Functions, Clamp, Duration Class, and More](https://msaied.com/articles/php-86-released-partial-functions-clamp-duration-class-and-more)  

   On this page
-------------

1. [The Problem: Policies and Query Scopes Drift Apart](#the-problem-policies-and-query-scopes-drift-apart)
2. [How Warrant Compares to Other Authorization Options](#how-warrant-compares-to-other-authorization-options)
3. [Installation](#installation)
4. [Define Conditions in a Schema](#define-conditions-in-a-schema)
5. [Filter Records Before Pagination](#filter-records-before-pagination)
6. [Return Per-Row Abilities](#return-per-row-abilities)
7. [Authorize Actions With Laravel's Gate](#authorize-actions-with-laravels-gate)
8. [Key Takeaways](#key-takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://msaied.com#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/832/68ae93269d6d7cdcbb51731f2a186f88.png) Filament · 3 min read### Filament v3.3.58 Released: Bug Fixes for Schemas, Actions, and Panel Context

21 Nov 2026 ](https://msaied.com/articles/filament-v3358-released-bug-fixes-for-schemas-actions-and-panel-context) [ ![](https://cdn.msaied.com/830/a9c10050839de78980aa9becf10b6786.png) PHP · 3 min read### PHP 8.6 Released: Partial Functions, Clamp, Duration Class, and More

19 Nov 2026 ](https://msaied.com/articles/php-86-released-partial-functions-clamp-duration-class-and-more) [ ![](https://cdn.msaied.com/829/1060437ce4d112be31b4254b41a2019c.png) Laravel · 3 min read### JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

17 Nov 2026 ](https://msaied.com/articles/json-schema-validation-rules-and-a-retryfor-attribute-in-laravel-1343) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://msaied.com#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://msaied.com)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://msaied.com/sitemap.xml)
