Filament Plugins

Purchase

Tenancy

If your codebase uses multi-tenancy, you need to ensure that the Media Library is aware of the current tenant when performing operations. Depending on your chosen driver, there are multiple approaches to achieve this.

MediaLibraryItem-driver

When using the Spatie 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.

1

Enabling tenancy

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.

2

Troubleshooting

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

Configuring tenant directory

When using the Filesystem Storage 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 ✦ All rights reserved.