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/ */
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
});