Filament Plugins

Purchase

Filters

You can use filters in the media library to narrow down the selection of media items based on specific criteria.

Default filters

By default, the Media Library comes with the following filters:

Date

The created at filter allows you to filter media items by their upload/creation date:

Created at filter

Size

The size filter allows you to filter media items by their size:

Size filter

Type

The mime type filter allows you to filter media items by their mime type:

Mime type filter

Tags

The tags filter allows you to filter media items by their associated tags:

Custom filter

Creating a custom filter requires several steps to link it up correctly with the driver. In this example, we'll create a filter that allows users to filter media items based on whether they are marked as "featured".

1

Create filter

First, the filter-class must be created. This class is responsible for rendering the filter form, generating indicators and defining which modifications should be applied when the filter is used.

app/MediaLibrary/Filters/FeaturedFilter.php

use App\MediaLibrary\Modifications\Scopes\FeaturedModification;
use RalphJSmit\Filament\Explore\Filters\Indicators\Indicator;

class FeaturedFilter extends Filter
{
    public function getFormSchemaComponents(): array
    {
        return [
            Forms\Components\Select::make('is_featured')
                ->label('Featured')
                ->placeholder('Select an option')
                ->options([
                    1 => 'Yes',
                    0 => 'No',
                ])
        ];
    }

    public function getIcon(): string | BackedEnum | null
    {
        return Heroicon::OutlinedStar;
    }

    public function getIndicators(array $data): array
    {
        $indicators = [];

        $isFeatured = $data['is_featured'] ?? null;

        if (filled($isFeatured)) {
            $label = $isFeatured ? 'Featured' : 'Not featured';
        
            $indicators[] = Indicator::make($this, $label)
                ->icon($this->getIcon())
                ->modifyDataUsing(function (array $data) {
                    $data['is_featured'] = null;

                    return $data;
                });
        }

        return $indicators;
    }

    public function getLabel(): string | Htmlable | null
    {
        return 'Featured';
    }

    public function getModifications(array $data): array
    {
        $modifications = [];
        
        $isFeatured = $data['is_featured'] ?? null;

        if (filled($isFeatured)) {
            $modifications[] = FeaturedModification::make()
                ->featured($isFeatured);
        }

        return $modifications;
    }
}
2

Register filter

Next, the filter must be registered on the library:

app/Providers/Filament/YourPanelProvider.php

$plugin
    ->filters([
        FeaturedFilter::make('featured'),
    ])
3

Create modification

Next, a modification needs to be created. A modification is returned from the filter, and is responsible for holding the "information" about what needs to be scoped:

app/MediaLibrary/Modifications/Scopes/FeaturedModification.php

use RalphJSmit\Filament\Explore\Drivers\Modifications\Modification;

class FeaturedModification extends Modification
{
    protected bool $isFeatured;

    public function featured(bool $isFeatured): static
    {
        $this->isFeatured = $isFeatured;

        return $this;
    }

    public function isFeatured(): bool
    {
        return $this->isFeatured;
    }
}
4

Apply modification

Finally, it must be told to the driver how to exactly apply the above modification:

app/Providers/Filament/YourPanelProvider.php

use App\MediaLibrary\Modifications\Scopes\FeaturedModification;

$plugin
    ->driver(MediaLibraryItemDriver::class, function (MediaLibraryItemDriver $driver) {
        $driver->applyModificationUsing(function (mixed $target, Modification $modification, Closure $next) {
            if ($modification instanceof FeaturedModification) {
                return $target->where('is_featured', $modification->isFeatured());
            }
            
            return $next($target, $modification);
        });
    });

If you have not yet made the choice for a driver, consider using the default recommended Media Library Item-driver. As this driver is database-based, you can easily add custom columns to the filament_media_library-table where you can conveniently store such extra meta-data as whether an item is featured.

This way, the filter will determine what modifications need to be applied when retrieving the media results, and the driver knows how to interpret these modifications and will apply them.

In the UI, the filter will now show up as such:

Custom filter

And when applied:

Custom filter indicator

You can read more about modifications and their types in the Modifications documentation.

© FilamentPlugins.com ✦ 2022 – 2025 ✦ All rights reserved.