Filament Plugins

Purchase

Multi-tenancy

If your codebase uses multi-tenancy, you need to ensure that the Media Library is aware of the current tenant when performing operations. The approach differs per driver — pick your driver from the tabs below for the relevant setup instructions.

Drivers

When using the Media Library Item driver in a multi-tenant application, you need to ensure that the Media Library is aware of the current tenant when performing operations. This can be achieved by enabling tenancy on the driver.

Media items are linked to tenants through tenant_* morph columns on the filament_media_library and filament_media_library_folders tables. When tenancy is enabled, the driver will automatically filter media items based on the current tenant.

To enable tenancy, call the ->tenancy() method on the $driver:

app/Providers/Filament/YourPanelProvider.php
$driver->tenancy()

In a future major version, tenancy will be automatically enabled based on your panel's tenancy status.

If you need to explicitly provide a tenant model instead of relying on Filament::getTenant(), you can use the ->tenant() method:

app/Providers/Filament/YourPanelProvider.php
$driver
    ->tenancy()
    ->tenant($tenantModel)

If you encounter the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'filament_media_library_folders.tenant_type' in 'where clause'

You are probably missing the V4 tenancy migration. Publish the migrations and run them:

bash
php artisan vendor:publish --tag="filament-media-library-migrations"
php artisan migrate

When using the Spatie Media Library driver in a multi-tenant application, you need to ensure that the Media Library is aware of the current tenant when performing operations. This can be achieved by enabling tenancy on the driver.

Media records are linked to tenants through tenant_type and tenant_id morph columns on the Spatie media table. When tenancy is enabled, the driver automatically stamps new uploads with the current tenant and scopes all queries to only return media belonging to that tenant.

1

Add tenant columns to the media table

The Spatie driver expects two morph columns — tenant_type and tenant_id — on the existing Spatie media table. Create a new migration to add them:

bash
php artisan make:migration add_tenancy_to_media_table
database/migrations/YYYY_MM_DD_add_tenancy_to_media_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('media', function (Blueprint $table) {
            $table->nullableMorphs('tenant');
        });
    }

    public function down(): void
    {
        Schema::table('media', function (Blueprint $table) {
            $table->dropMorphs('tenant');
        });
    }
};

Then run the migration:

bash
php artisan migrate

The columns are nullable so existing media records won't be affected. Files uploaded before enabling tenancy remain accessible to all tenants until you backfill the columns yourself.

2

Enabling tenancy

To enable tenancy, call the ->tenancy() method on the $driver:

app/Providers/Filament/YourPanelProvider.php
$driver->tenancy()

With tenancy enabled, the driver will:

  • Stamp tenant_type and tenant_id on every new upload using the current tenant returned by Filament::getTenant().
  • Scope every query for files (getFiles(), findFile(), findFiles(), paginated results) to only return media belonging to the current tenant.

In a future major version, tenancy will be automatically enabled based on your panel's tenancy status.

If you need to explicitly provide a tenant model instead of relying on Filament::getTenant(), you can use the ->tenant() method:

app/Providers/Filament/YourPanelProvider.php
$driver
    ->tenancy()
    ->tenant($tenantModel)

When using the Storage/disk driver in a multi-tenant application, you can configure tenancy by dynamically setting the ->directory() based on the current tenant.

To isolate media files per tenant, pass a closure to the driver configuration that sets the ->directory() based on the current tenant:

app/Providers/Filament/YourPanelProvider.php
$plugin
    ->driver(FilesystemStorageDriver::class, function (FilesystemStorageDriver $driver) {
        return $driver
            ->disk('s3')
            ->directory('media/' . Filament::getTenant()->getKey());
    })
© FilamentPlugins.com ✦ 2022 – 2026
PrivacyTerms & Conditions
All rights reserved.