PHP code example of radiergummi / laravel-rls

1. Go to this page and download the library: Download radiergummi/laravel-rls library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

radiergummi / laravel-rls example snippets


// bootstrap/providers.php
return [
    App\Providers\AppServiceProvider::class,
    App\Providers\RlsServiceProvider::class,
];

class RlsServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // Declare your isolation keys. This enables value validation and typed
        // SQL helpers (rls.tenant_id()) generated from the declared type.
        Rls::defineContext(fn (ContextSchema $schema) => $schema->uuid('tenant_id'));

        // Map an authenticated identity to its scope. Called on Laravel's
        // Authenticated event, so a logged-in request establishes context on
        // its own. Return an array of isolation key => value.
        Rls::resolveContextUsing(fn ($user) => ['tenant_id' => $user->tenant_id]);
    }
}

Schema::create('documents', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->uuid('tenant_id');
    $table->string('title');

    $table->isolatedBy('tenant_id');
});

$table->isolatedBy('tenant_id')->withDefault();

Document::all();            // only the current tenant's rows
Document::find($id);        // null if that id belongs to another tenant
Document::create([...]);    // rejected by WITH CHECK if it would write another tenant's row

Rls::isolateTo(['tenant_id' => $tenant->id], function () {
    Document::create([...]);   // scoped to $tenant
});

Rls::withoutIsolation('nightly-export', fn () => Document::all());

Rls::defineContext(fn (ContextSchema $schema) => $schema
    ->uuid('org_id')
    ->integer('region_id'));

// in a migration
$table->isolatedBy('org_id');
$table->isolatedBy('region_id', type: 'integer');

// establishing a compound scope
Rls::isolateTo(['org_id' => $org->id, 'region_id' => 3], fn () => Report::all());

// stancl/tenancy
Rls::resolveContextUsing(fn () => tenant()
    ? ['tenant_id' => tenant()->getTenantKey()]
    : []);

// spatie/laravel-multitenancy
Rls::resolveContextUsing(fn () => Tenant::current()
    ? ['tenant_id' => Tenant::current()->getKey()]
    : []);

use Radiergummi\LaravelRls\Testing\InteractsWithRls;

class DocumentIsolationTest extends TestCase
{
    use InteractsWithRls;

    public function test_a_tenant_cannot_see_another_tenants_documents(): void
    {
        $this->assertIsolates(
            Document::class,
            isolatedBy: 'tenant_id',
            acting: $tenantA,
            cannotSee: $tenantB,
        );

        $this->assertRejectsForeignWrite(
            Document::class,
            isolatedBy: 'tenant_id',
            acting: $tenantA,
            foreign: $tenantB,
        );
    }
}
bash
php artisan migrate