Looking to hire Laravel developers? Try LaraJobs

laravel-postgresql-rls maintained by tpetry

Description
Row-level security for Laravel applications using PostgreSQL
Author
Last update
2026/11/16 15:02 (dev-main)
License
Downloads
0

Comments
comments powered by Disqus

Row-level security for Laravel applications using PostgreSQL

Installation

You can install the package via Composer:

composer require tpetry/laravel-postgresql-rls

Usage

Register an identity resolver, e.g. in the boot() method of your AppServiceProvider, which returns the claims for the RLS policies:

use Illuminate\Support\Facades\Auth;
use Tpetry\PostgresqlRls\Facades\RlsIdentityResolver;
use Tpetry\PostgresqlRls\RlsIdentity;

RlsIdentityResolver::register(fn (array $params, array $context) => new RlsIdentity(
    claims: ['tenant_id' => Auth::user()->tenant_id],
));

All queries within the authenticated() closure are executed within a transaction with those claims applied:

use Tpetry\PostgresqlRls\Facades\Rls;

$invoices = Rls::authenticated(function () {
    return Invoice::all();
});

The claims are available to your RLS policies as a JSON object in the rls.claims setting, e.g. for a policy created with the row-level security migrations:

use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;

Schema::table('invoices', function (Blueprint $table) {
    $table->rowLevelSecurity();
    $table->createPolicy(
        name: 'tenant_isolation',
        user: 'app',
        condition: "tenant_id = (SELECT NULLIF(current_setting('rls.claims', true), '')::jsonb ->> 'tenant_id')::bigint",
    );
});

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

License

Laravel PostgreSQL Row-Level Security is open-sourced software licensed under the MIT license.