Filament Plugins

Purchase

Spatie Media Library driver

The SpatieMediaLibraryDriver lets you browse and manage your existing Spatie Media Library records directly inside the Media Library UI – organised into virtual folders by collection, model type, and individual model.

Installation

1

Install Spatie Media Library

The SpatieMediaLibraryDriver requires the Spatie Media Library package. If you haven't installed it yet, require it now and follow its setup instructions:

terminal
composer require spatie/laravel-medialibrary

Next, publish and run the Spatie migrations:

terminal
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations"
php artisan migrate

If Spatie Media Library is already set up in your application, you can skip this step.

2

Register the driver

Open up your panel provider and register the SpatieMediaLibraryDriver on the plugin:

app/Providers/Filament/YourPanelProvider.php
use RalphJSmit\Filament\MediaLibrary\Drivers\SpatieMediaLibraryDriver;

$plugin
    ->driver(SpatieMediaLibraryDriver::class)

The driver has no required configuration beyond this – it will read from whatever Spatie media records already exist in your database.

3

Configure groupables

By default the driver shows all media records in a flat list. To organise them into virtual folders, pass an array of groupables via ->spatieMediaLibraryGroupables().

Each entry in the array represents one level of virtual folder depth, from the outermost (index 0) to the innermost (last index). The three available groupable types are:

Type Groups by
SpatieMediaGroupable::Collection Spatie collection name
SpatieMediaGroupable::ModelType Class of the owning model
SpatieMediaGroupable::Model Individual model instance

A typical setup that produces three levels of folders – Collection → Model type → Model – looks like this:

app/Providers/Filament/YourPanelProvider.php
use RalphJSmit\Filament\MediaLibrary\Drivers\SpatieMediaLibraryDriver\SpatieMediaGroupable;

$driver->spatieMediaLibraryGroupables([
    SpatieMediaGroupable::Collection,
    SpatieMediaGroupable::ModelType,
    SpatieMediaGroupable::Model,
])

Files can only be uploaded when the current folder resolves to a specific model instance (a Model groupable), because Spatie Media Library requires a concrete model to attach the media to.

By default the driver surfaces all media records from your database. Use the scoping methods below to limit which records are visible.

Restricting by model type

Pass an array of fully-qualified model class names to ->modelTypes(). Only media belonging to those models will appear:

app/Providers/Filament/YourPanelProvider.php
use App\Models\Post;
use App\Models\Page;

$driver->modelTypes([Post::class, Page::class])

The driver resolves morph map aliases automatically, so you don't need to pass the alias string – the class name is always correct.

Restricting by collection

Pass an array of collection name strings to ->collections(). Only media in those collections will appear:

app/Providers/Filament/YourPanelProvider.php
$driver->collections(['images', 'documents'])

Combining both

You can combine ->modelTypes() and ->collections() freely. Both constraints are applied at the database query level, so only media matching both criteria is loaded:

app/Providers/Filament/YourPanelProvider.php
use App\Models\Post;

$driver
    ->modelTypes([Post::class])
    ->collections(['images', 'thumbnails'])

The driver derives folder and file-info labels automatically, but you can override every label to match your application's language and branding.

Model instance labels

By default the driver uses the model's name attribute, or falls back to #<id> when no name column exists. Override this per-model by calling ->getModelLabelUsing() with a closure that receives the model instance and returns a string (or null to fall through to the next registered callback):

app/Providers/Filament/YourPanelProvider.php
use App\Models\Post;

$driver->getModelLabelUsing(function ($model): ?string {
    if ($model instanceof Post) {
        return $model->title;
    }

    return null;
})

You may call ->getModelLabelUsing() multiple times. The driver tries each callback in order and uses the first non-null value.

Model type labels

The folder label for a ModelType groupable defaults to the plural Filament model label (e.g. "Posts"). Override it with ->getModelTypeLabelUsing(), which receives the fully-qualified class name:

app/Providers/Filament/YourPanelProvider.php
use App\Models\Post;

$driver->getModelTypeLabelUsing(function (string $modelClass): ?string {
    if ($modelClass === Post::class) {
        return 'Blog posts';
    }

    return null;
})

Collection labels

Collection names default to Str::ucfirst($collectionName) (e.g. "featured_images""Featured_images"). You have three ways to customise them, in increasing order of specificity:

For all collections at once, use ->getCollectionLabelUsing():

app/Providers/Filament/YourPanelProvider.php
$driver->getCollectionLabelUsing(function (string $collection): string {
    return str($collection)->replace('_', ' ')->title()->toString();
})

For a single collection, use ->collectionLabel():

app/Providers/Filament/YourPanelProvider.php
$driver->collectionLabel('featured_images', 'Featured images')

For several collections at once, use ->collectionLabels():

app/Providers/Filament/YourPanelProvider.php
$driver->collectionLabels([
    'featured_images' => 'Featured images',
    'og_images'       => 'OG images',
])

When both ->collectionLabels() and ->getCollectionLabelUsing() are configured, the per-collection label takes priority; the callback is only called when no explicit label is found.

Making the model name clickable

In the file info panel, the driver shows which model the media is attached to. By default this is plain text. Call ->getModelUrlUsing() to turn it into a link – useful for jumping straight to the Filament resource edit page:

app/Providers/Filament/YourPanelProvider.php
use App\Models\Post;
use App\Filament\Resources\PostResource;

$driver->getModelUrlUsing(function ($model): ?string {
    if ($model instanceof Post) {
        return PostResource::getUrl('edit', ['record' => $model]);
    }

    return null;
})

Return null for models where you don't want a link. Like ->getModelLabelUsing(), you can call ->getModelUrlUsing() multiple times and the first non-null result wins.

The SpatieMediaLibraryDriver is designed around the structure Spatie Media Library imposes. That structure comes with a few constraints worth understanding before you commit to this driver.

No folder creation or moving

Virtual folders are derived entirely from media record attributes – they are not stored anywhere. Because of that, the driver cannot create new folders, rename virtual folders, or move files between folders. Attempting to do so throws a RuntimeException. The related UI actions (create folder, move) are automatically hidden.

Uploading a file is only possible when the current folder resolves to a specific model instance (a Model groupable). The upload is then attached to that model under the resolved collection name.

Media records are not re-usable

Each Spatie media record can only be attached to one model at a time. If you need to link the same file to multiple models, this driver is not the right fit.

The Media Library Item-driver solves this by placing an intermediate MediaLibraryItem model between your models and the Spatie media records, making files fully re-usable across your application.

That's it! You now have a fully functional Media Library that surfaces your existing Spatie Media Library records – organised into virtual folders by collection, model type, and model – with no changes required to your existing data.

If you need re-usable media items that can be linked to multiple models, take a look at the Media Library Item-driver instead.

© FilamentPlugins.com ✦ 2022 – 2026
PrivacyTerms & Conditions
All rights reserved.