### Setup Custom Theme and CSS Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/installation Generates a Filament custom theme and imports the required Media Library CSS styles into the project. ```bash php artisan make:filament-theme ``` ```css @import '../../../../vendor/ralphjsmit/laravel-filament-media-library/resources/css/index.css'; ``` -------------------------------- ### Configure Composer Repository Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/installation Adds the private RalphJSmit composer repository to your project configuration to allow installation of premium packages. ```bash composer config 'repositories.ralphjsmit/*' composer https://satis.ralphjsmit.com ``` ```json { "repositories": [ { "type": "composer", "url": "https://satis.ralphjsmit.com" } ] } ``` -------------------------------- ### Configure authentication for Laravel Cloud Source: https://filamentplugins.com/filament-media-library-pro/docs Adds private repository credentials to the deployment script for Laravel Cloud environments. Replace placeholders with your actual license credentials. ```bash composer config http-basic.satis.ralphjsmit.com {username} {password} composer install --no-dev ``` -------------------------------- ### Require Filament Media Library Pro Package Source: https://filamentplugins.com/filament-media-library-pro/docs Installs the Filament Media Library Pro package using Composer. This command will prompt for authentication credentials (username and license key). ```bash composer require ralphjsmit/laravel-filament-media-library:^4.0 ``` -------------------------------- ### Set Minimum Stability to Beta Source: https://filamentplugins.com/filament-media-library-pro/docs Configures Composer to allow beta versions, which is required for the current version of the Media Library. This setting is crucial for successfully installing the package. ```bash composer config minimum-stability beta ``` -------------------------------- ### Set Minimum Stability Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/installation Configures the project to accept beta versions of packages, which is required for the Media Library V4. ```bash composer config minimum-stability beta ``` ```json { "minimum-stability": "beta" } ``` -------------------------------- ### Configure Composer JSON for Minimum Stability Source: https://filamentplugins.com/filament-media-library-pro/docs Shows the composer.json configuration after setting the minimum stability to beta. This allows Composer to fetch pre-release versions of packages. ```json { "minimum-stability": "beta" } ``` -------------------------------- ### Create Filament Custom Theme Source: https://filamentplugins.com/filament-media-library-pro/docs Generates a new Filament custom theme for your project. This is a prerequisite for integrating the Media Library's styling. ```bash php artisan make:filament-theme ``` -------------------------------- ### Install Media Library Driver Dependencies Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/filament-media-library Commands to publish migrations and install the required Spatie Media Library package for the Filament driver. ```bash php artisan vendor:publish --tag="filament-media-library-migrations" composer require spatie/laravel-medialibrary php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" ``` -------------------------------- ### Configure authentication for GitHub Actions Source: https://filamentplugins.com/filament-media-library-pro/docs Configures the Composer repository credentials within a GitHub Actions workflow. Requires setting the FILAMENT_MEDIA_LIBRARY_USERNAME and PASSWORD secrets. ```yaml composer config http-basic.satis.ralphjsmit.com ${{ secrets.FILAMENT_MEDIA_LIBRARY_USERNAME }} ${{ secrets.FILAMENT_MEDIA_LIBRARY_PASSWORD }} composer install --no-interaction --prefer-dist --ignore-platform-reqs --no-dev ``` -------------------------------- ### Register Filament Media Library Plugin Source: https://filamentplugins.com/filament-media-library-pro/docs Registers the Filament Media Library plugin within your panel provider. This makes the Media Library available in the specified Filament panel. ```php use RalphJSmit\Filament\MediaLibrary\FilamentMediaLibrary; $panel ->plugins([ FilamentMediaLibrary::make(), ]) ``` -------------------------------- ### Set Specific Folder for MediaPicker (Closure Example) Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Demonstrates how to dynamically set a specific folder for the MediaPicker using a closure. This allows the folder to be determined based on the current record or other contextual data, providing flexibility in folder scoping. ```php MediaPicker::make('featured_image_id') ->folder(fn (Event $record) => $record->mediaLibraryFolder); ``` -------------------------------- ### Add Private Composer Repository Source: https://filamentplugins.com/filament-media-library-pro/docs Adds a private Composer repository to your project's composer configuration. This is necessary to access the Filament Media Library Pro package. Ensure the repository URL is correct. ```bash composer config "repositories.ralphjsmit/*" composer https://satis.ralphjsmit.com ``` -------------------------------- ### Configure Composer JSON for Private Repository Source: https://filamentplugins.com/filament-media-library-pro/docs Illustrates the expected structure of the composer.json file after adding the private Composer repository. This ensures Composer can locate the package. ```json { "repositories": [ { "type": "composer", "url": "https://satis.ralphjsmit.com" } ] } ``` -------------------------------- ### Publish translations for Filament Media Library Source: https://filamentplugins.com/filament-media-library-pro/docs Command to publish translation files to your Laravel application. This allows for customization of the package's language strings. ```bash php artisan vendor:publish --tag="filament-media-library-translations" ``` -------------------------------- ### Import Media Library CSS into Custom Theme Source: https://filamentplugins.com/filament-media-library-pro/docs Includes the Media Library's CSS file into your custom Filament theme. Adjust the path based on your theme's location relative to the vendor directory. ```css @import '../../../../vendor/ralphjsmit/laravel-filament-media-library/resources/css/index.css'; ``` -------------------------------- ### Implement Custom Bulk Actions in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/bulk-actions This PHP code demonstrates how to define custom bulk actions for the Filament Media Library Pro using the `->bulkActions()` method. It includes examples of using pre-built actions, creating custom actions with icons and logic, and grouping actions. Custom actions must extend `RalphJSmitFilamentExploreFilamentActionsBulkAction`. ```php use Illuminate\Support\Collection; use Filament\Support\Actions\ActionGroup; use RalphJSmit\Filament\Explore\Filament\Actions\BulkAction; use RalphJSmit\Filament\Explore\Filament\Actions\CreateFolderBulkAction; use RalphJSmit\Filament\Explore\Filament\Actions\DeleteBulkAction; use RalphJSmit\Filament\Explore\Filament\Actions\MoveBulkAction; use Heroicon\Icon as Heroicon; $plugin ->bulkActions([ MoveBulkAction::make(), DeleteBulkAction::make(), BulkAction::make('optimize') ->icon(Heroicon::OutlinedSparkles) ->action(function (Collection $files) { // Perform an action... }), ActionGroup::make([ CreateFolderBulkAction::make(), ]) ->icon(Heroicon::OutlinedEllipsisHorizontal) ->color('gray'), ]); ``` -------------------------------- ### Configure Dynamic Scoped Folder using Closure in Filament Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/scoped-folder This snippet shows how to configure a dynamic scoped folder for the Filament Media Library Pro plugin using a closure. This allows the folder path to be determined at runtime, for example, based on the current tenant's key, ensuring user-specific media storage. ```php $plugin->scopedFolder(fn () => FileData::fromDisk('s3', "tenants/" . Filament::getTenant()->getKey(), FileType::Folder)) ``` -------------------------------- ### Enable Video Uploads in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/accepted-file-types This snippet shows how to enable video uploads in the Filament Media Library Pro with the `acceptVideo()` method. It requires the FFmpeg binary to be installed and the 'php-ffmpeg/php-ffmpeg' package, along with updated configuration for `ffmpeg_path` and `ffmpeg_probe`. ```php $plugin->acceptVideo() ``` -------------------------------- ### Enable Spatie Tags Integration in Filament Media Library Pro V4 Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide This snippet shows how to enable the built-in spatie/laravel-tags integration in Filament Media Library Pro V4. It replaces the manual trait addition and custom model item registration. Ensure `spatie/laravel-tags` is installed. ```php $plugin ->spatieTagsIntegration() // Remove if the only use case was adding the `HasTags` trait: ->modelItem("App\\Models\\MediaLibraryItem::class") ``` ```php MediaLibrary::registerMediaInfoFormFields(fn (array $schema): array => [ ...$schema, SpatieTagsInput::make('tags'), ]); ``` ```php class MediaLibraryItem extends \RalphJSmit\Filament\MediaLibrary\Media\Models\MediaLibraryItem { use HasTags; } ``` -------------------------------- ### Configure MediaPicker with Spatie Media Library Driver Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/drivers This snippet shows how to configure the MediaPicker component to use the Spatie Media Library driver. It allows users to select existing media files linked to the model and upload new ones. This driver is suitable for existing Spatie Media Library setups but does not support file reusability across models. ```php MediaPicker::make('featured_image') ->label('Featured image') ->required(); ``` -------------------------------- ### Enable PDF Uploads in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/accepted-file-types This snippet demonstrates how to enable PDF uploads in the Filament Media Library Pro using the `acceptPdf()` method. Note that this requires the 'spatie/pdf-to-image' package to be configured correctly, including imagick extension and Ghostscript installation. ```php $plugin->acceptPdf() ``` -------------------------------- ### Register Media Conversions using MediaLibrary Facade and Driver Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide Demonstrates the migration of media conversion registration from the legacy MediaLibrary facade to the new driver-based approach. The code defines custom image dimensions and sharpening settings for media items. ```PHP MediaLibrary::registerMediaConversions(function (MediaLibraryItem $mediaLibraryItem) { $mediaLibraryItem->addMediaConversion('custom')->width(368)->height(232)->sharpen(10); }); ``` ```PHP $plugin ->driver(modifyDriverUsing: function (MediaLibraryItemDriver $driver) { $driver ->registerConversions(function (MediaLibraryItem $mediaLibraryItem) { $mediaLibraryItem->addMediaConversion('custom')->width(368)->height(232)->sharpen(10); }); }); ``` -------------------------------- ### Configure Filament Media Library Plugin Methods Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide Review and update the methods called on the `FilamentMediaLibrary::make()` object for each panel. This includes setting disk visibility, navigation icons, and model configurations. Dependencies include the `RalphJSmitFilamentMediaLibrary` namespace. ```php use RalphJSmitFilamentMediaLibraryFilamentMediaLibrary; use RalphJSmitFilamentMediaLibraryDriversMediaLibraryItemDriver; use SpatieMediaLibraryConversionsConversion; use Heroicon; FilamentMediaLibrary::make() ->diskVisibilityPrivate() // Automatically detected disk visibility now ->diskVisibility('private') // Automatically detected disk visibility now ->diskVisibilityPublic() // Automatically detected disk visibility now ->diskVisibility('public') // Automatically detected disk visibility now // Optional, to stay in line with Filament best practices: ->navigationIcon('heroicon-o-video-camera') ->activeNavigationIcon('heroicon-s-video-camera') ->navigationIcon(Heroicon::OutlinedVideoCamera) ->activeNavigationIcon(Heroicon::VideoCamera) ->modelItem(App\Models\MediaLibraryItem::class) ->modelFolder(App\Models\MediaLibraryItem::class) ->driver(modifyDriverUsing: function (MediaLibraryItemDriver $driver) { $driver ->mediaLibraryItemModel(App\Models\MediaLibraryItem::class) ->mediaLibraryFolderModel(App\Models\MediaLibraryFolder::class); }) ->firstAvailableUrlConversions(...) ``` -------------------------------- ### Run Migrations for Tenancy Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/multi-tenancy Commands to publish and run missing migrations required for multi-tenancy database columns in Filament Media Library. ```bash php artisan vendor:publish --tag="filament-media-library-migrations" php artisan migrate ``` -------------------------------- ### Configure Media Conversions with Glide Integration Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide Configure media conversions, opting in to conversions using `$driver->conversions()` to leverage Glide for on-the-fly image generation. This section shows how to enable and modify responsive, medium, small, and thumbnail conversions. Dependencies include `Spatie\MediaLibrary\Conversions\Conversion` and `RalphJSmit\Filament\MediaLibrary\Drivers\MediaLibraryItemDriver`. ```php $plugin // Copy the below method configuration (if any)... ->conversionResponsive(enabled: true, modifyUsing: function (Conversion $conversion) { // Apply any modifications you want to the conversion, or omit to use defaults... return $conversion->keepOriginalImageFormat(); }) ->conversionMedium(enabled: true, width: 800) ->conversionSmall(enabled: true, width: 400) ->conversionThumb(enabled: true, width: 600, height: 600) ->driver(modifyDriverUsing: function (MediaLibraryItemDriver $driver) { $driver ->conversions() // And move them into the `$driver` instead... ->conversionResponsive(enabled: true, modifyUsing: function (Conversion $conversion) { // Apply any modifications you want to the conversion, or omit to use defaults... return $conversion->keepOriginalImageFormat(); }) ->conversionMedium(enabled: true, width: 800) ->conversionSmall(enabled: true, width: 400) ->conversionThumb(enabled: true, width: 600, height: 600); }) // The following methods can also be removed: ->thumbnailMediaConversion('thumb') ->mediaPickerMediaConversion('thumb') ``` -------------------------------- ### Configure Filament Media Library Actions Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/principles Demonstrates how to instantiate and globally configure specific media library actions such as creating folders, renaming, or uploading files using the configureUsing method. ```php use RalphJSmit\Filament\Explore\Filament\Actions\CreateFolderAction; CreateFolderAction::configureUsing(function (CreateFolderAction $action) { $action ->label('New directory') ->successNotificationTitle(function (array $data) { return 'Awesome, your directory ' . $data['name'] . ' was created!'; }); }); ``` -------------------------------- ### Enable Tenancy for MediaLibraryItem Driver Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/multi-tenancy Enables multi-tenancy support for the MediaLibraryItem driver to automatically filter media items by the current tenant using morph columns. ```php $driver->tenancy() ``` -------------------------------- ### Configure and Modify Media Library Schemas Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/configurable-schemas Demonstrates how to hook into a configurable schema to retrieve existing components and inject a custom action into the UI structure. This approach allows for deep customization of the component tree, such as wrapping actions in groups or adding new elements. ```php FileTopbarSchema::configureUsing(function (FileTopbarSchema $configurableSchema) { $components = $configurableSchema->getConfiguredComponents(); // Modify $components... $configurableSchema->configuredComponents($components); }); /** @var Section $sectionComponent */ $sectionComponent = $components[0]; /** @var Flex $flexComponent */ $flexComponent = $sectionComponent->getChildComponents()[0]; [$breadcrumbsComponent, $createFolderAction] = $flexComponent->getChildComponents(); $configurableSchema->configuredComponents([ $sectionComponent ->schema([ $flexComponent ->schema([ $breadcrumbsComponent, // Wrap the actions in a group, so that they stay together... Group::make([ CustomAction::make(), $createFolderAction, ]), ]), ]), ]); ``` -------------------------------- ### Set Media Library Navigation Badge Color (String) Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/media-library-page Customize the color of the navigation badge using a predefined color string with the `->navigationBadgeColor()` method. Examples include 'gray', 'red', 'blue', etc. ```php $plugin->navigationBadgeColor('gray') ``` -------------------------------- ### Programmatic File Upload with Filament Media Library Driver Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide Programmatically adding files now requires instantiating the driver instead of calling `MediaLibraryItem::addUpload(...)`. The driver provides methods like `createFile` for uploads. ```php use Illuminate\Http\UploadedFile; use RalphJSmit\Filament\MediaLibrary\Models\MediaLibraryItem; use Filament\Facades\Filament; use RalphJSmit\Filament\MediaLibrary\FilamentMediaLibrary; use RalphJSmit\Filament\Explore\Data\TemporaryFileUploadData; $uploadedFile = /** */; $mediaItem = MediaLibraryItem::addUpload($uploadedFile); $driver = Filament::getPanel('admin') ->getPlugin(FilamentMediaLibrary::make()->getId()) ->getDriver(); $fileData = $driver->createFile( folder: null, temporaryFileUploadData: TemporaryFileUploadData::fromUploadedFile($uploadedFile) ); ``` ```php $mediaLibraryFolder = MediaLibraryFolder::find(1); // However you get the folder... $fileData = $driver->createFile( folder: FileData::fromMediaLibraryFolder($mediaLibraryFolder), temporaryFileUploadData: TemporaryFileUploadData::fromUploadedFile($uploadedFile) ); ``` ```php $fileData = $driver->createFile( folder: null, temporaryFileUploadData: TemporaryFileUploadData::fromDisk( disk: 's3', path: 'path/to/file.jpg', shouldPreserveOriginalFile: true // Default false, can be set to true if you want to keep the original file on the disk ), ); ``` -------------------------------- ### Manage Folders and Advanced Operations Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/programmatic-upload Demonstrates how to perform advanced file system tasks such as creating folders, moving, renaming, and deleting files using the driver. ```php $fileData = $driver->createFile($folder, $temporaryFileUploadData); $fileData = $driver->createFolder($folder, 'New folder'); $driver->deleteFile($fileData); $driver->moveFile($fileData, $toFolder); $driver->renameFile($fileData, 'new-name.jpg'); $files = $driver->getFiles($folder, $modifications); ``` -------------------------------- ### Implementing applyModification in custom drivers Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/modifications Shows how to implement the applyModification method directly within a custom driver class. This is the recommended approach for custom or extended drivers to handle specific modification types. ```php class CustomDriver extends Driver { /** * @param YourTargetObject $target */ public function applyModification(mixed $target, Modification $modification): mixed { if ($modification instanceof PublishedModification) { return $target->where('is_published', $modification->isPublished()); } return parent::applyModification($modification, $target); } } ``` -------------------------------- ### Disable Filament Upload Pro Integration Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/integrations/upload-pro-integration This code snippet demonstrates how to disable the Filament Upload Pro integration with the Media Library upload field. It uses the `->uploadProIntegration(false)` method on the plugin configuration. This is useful if you have the Upload package installed but prefer not to use its integration with the Media Library. ```php $plugin ->uploadProIntegration(false) ``` -------------------------------- ### Instantiate Media Library Driver Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/programmatic-upload Retrieves the media library driver instance from the Filament panel plugin configuration. ```php use Filament\Facades\Filament; use RalphJSmit\Filament\MediaLibrary\FilamentMediaLibrary; $driver = Filament::getPanel('admin') ->getPlugin(FilamentMediaLibrary::make()->getId()) ->getDriver(); ``` -------------------------------- ### Configure Custom Driver Dynamically Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/custom-drivers This snippet demonstrates how to register a custom driver class and apply dynamic configuration settings using a closure within the Filament panel provider. It allows for flexible driver behavior by exposing public methods on the driver class. ```php $plugin ->driver(CustomMediaLibraryDriver::class, function (CustomMediaLibraryDriver $driver) { // ... }) ``` -------------------------------- ### Enable Bulk Actions for Media Picker Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Shows how to enable bulk selection and actions on multiple media items in the MediaPicker using the `->bulkSelectable()` and `->bulkActions()` methods. This is useful for batch processing selected files. ```php use RalphJSmit\Filament\Explore\Filament\Actions\BulkAction; use Filament\Support\Facades\Heroicon; use Illuminate\Database\Eloquent\Collection; MediaPicker::make('images') ->label('Choose images') ->multiple() ->bulkSelectable() ->bulkActions([ BulkAction::make('optimize') ->icon(Heroicon::OutlinedSparkles) ->action(function (Collection $files) { // Perform an action... }), ]) ``` -------------------------------- ### Create Custom Media Library Driver in PHP Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/custom-drivers Defines a custom media library driver by extending the abstract `Driver` class. This involves implementing methods to manage files and folders, such as creating, deleting, finding, and moving them. It allows for custom logic to interact with various storage backends or data sources. ```php use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use RalphJSmit\Filament\Explore\Data\FileData; use RalphJSmit\Filament\Explore\Data\TemporaryFileUploadData; use RalphJSmit\Filament\Explore\Drivers\Driver; use RalphJSmit\Filament\Explore\Enums\FileType; class CustomMediaLibraryDriver extends Driver { /** * If `true`, then adding a directory separator to a folder name does not move the folder. * If `false`, then adding or removing a directory separator to a folder name will move * the folder and result in a folder name as after the last directory separator. */ public function allowsDirectorySeparatorInFolderName(): bool { // } public function createFile(?FileData $folder, TemporaryFileUploadData $temporaryFileUploadData): FileData { // } public function createFolder(?FileData $folder, string $name): FileData { // } public function deleteFile(FileData $file): void { // } public function findFile(?FileType $fileType, string $key, array $modifications = []): ?FileData { // } /** * @return Collection */ public function findFiles(?FileType $fileType, array $keys, array $modifications = []): Collection { // } public function getDirectorySeparator(): string { // } /** * @return Collection */ public function getFiles(?FileData $folder = null, array $modifications = []): Collection { // } public function getFilesCount(?FileData $folder = null, array $modifications = []): int { // } public function getFilesPaginator(?FileData $folder = null, array $modifications = [], int $page = 1, int $perPage = 50): LengthAwarePaginator { // } public function getMaxFileSizeKb(): int { // } public function moveFile(FileData $file, ?FileData $to): void { // } } ``` -------------------------------- ### Update MediaLibraryItem and MediaLibraryFolder Model Imports Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide If you import `MediaLibraryItem` or `MediaLibraryFolder` models, update their namespaces to reflect the new structure in Filament Media Library Pro. ```php use RalphJSmit\Filament\MediaLibrary\Media\Models\MediaLibraryItem; use RalphJSmit\Filament\MediaLibrary\Media\Models\MediaLibraryFolder; use RalphJSmit\Filament\MediaLibrary\Models\MediaLibraryItem; use RalphJSmit\Filament\MediaLibrary\Models\MediaLibraryFolder; ``` -------------------------------- ### Configure Tenant-Specific Storage Directory Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/multi-tenancy Dynamically sets the storage directory for the FilesystemStorageDriver based on the current tenant's unique identifier to ensure file isolation. ```php $plugin ->driver(FilesystemStorageDriver::class, function (FilesystemStorageDriver $driver) { return $driver ->disk('s3') ->directory('media/' . Filament::getTenant()->getKey()); }) ``` -------------------------------- ### Optimizing MediaColumn with Relationships Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-column To prevent N+1 query issues when using the MediaLibraryItem driver, use the relationship method to eager load the media item associated with the record. ```php MediaColumn::make('featuredImage') ->relationship() ->label('Featured image') ``` -------------------------------- ### Implement MediaLibraryItemDriver in Filament Forms and Models Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/drivers Demonstrates how to use the default MediaLibraryItemDriver in a Filament form field, define the corresponding Eloquent relationship in a model, and display the image in a Blade template. ```php MediaPicker::make('featured_image_id') ->label('Featured image') ->required(); ``` ```php class YourModel extends Model { public function featuredImage(): BelongsTo { return $this->belongsTo(MediaLibraryItem::class, 'featured_image_id'); } } ``` ```blade {{ $yourModel->featuredImage->name }} ``` -------------------------------- ### Customize File Actions in Filament Panel Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/principles Shows how to inject custom actions or modify existing ones within the Filament panel provider, allowing for extended functionality like file optimization. ```php use Filament\Actions\ActionGroup; use RalphJSmit\Filament\Explore\Filament\Actions\DeleteAction; use RalphJSmit\Filament\Explore\Filament\Actions\Action; $plugin ->fileActions(fn () => [ DeleteAction::make(), ActionGroup::make([ Action::make('optimize') ->icon(Heroicon::OutlinedSparkles) ->action(function (FileData $file) { // ... }), ]), ]); ``` -------------------------------- ### Create Pivot Table Migration for BelongsToMany Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Provides the necessary Artisan command and migration code to create a pivot table for `BelongsToMany` relationships when using the Media Library Item Driver. This table links models to media library items. ```terminal php artisan make:migration create_post_media_library_item_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::create('media_library_item_post', function (Blueprint $table) { $table->id(); $table->foreignId('post_id')->constrained()->cascadeOnDelete(); $table->foreignId('media_library_item_id')->constrained()->cascadeOnDelete(); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('media_library_item_post'); } }; ``` ```terminal php artisan migrate ``` -------------------------------- ### Update MediaEntry Component Configuration Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide Review instances of the `MediaEntry` in your infolists. If using a relationship, you must now explicitly add the `->relationship()` method. Alternatively, it can be used without a relationship by providing the field name directly or referencing the foreign key. Dependencies include `RalphJSmit\Filament\MediaLibrary\Infolists\Components\MediaEntry` and Eloquent relationships. ```php use RalphJSmit\Filament\MediaLibrary\Infolists\Components\MediaEntry; MediaEntry::make('featuredImage') ->relationship() ``` ```php MediaEntry::make('featuredImage') MediaEntry::make('featured_image_id') public function featuredImage(): BelongsTo { return $this->belongsTo(MediaLibraryItem::class, 'featured_image_id'); } ``` -------------------------------- ### Redirect to specific folder in Media Library Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/default-folder-opening Demonstrates how to generate a URL for the Media Library page that automatically opens a specific folder. This requires the Media Library Item Driver and uses the getUrl method with a folder query parameter. ```php use RalphJSmit\Filament\MediaLibrary\Filament\Pages\MediaLibrary; $url = MediaLibrary::getUrl(['folder' => 3]); // {panel}/media-library?folder=3 ``` -------------------------------- ### Configure Media Storage Disk Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/filament-media-library Set the environment variable to specify the storage disk used by the Media Library. ```env MEDIA_DISK=s3 ``` -------------------------------- ### Enable Downloading of Selected Media Items Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Adds a download button to the MediaPicker, allowing users to download selected media items. This feature is useful for providing direct access to files managed through the Media Library. ```php MediaPicker::make('image') ->label('Featured image') ->downloadable() ``` -------------------------------- ### Register and Configure FilesystemStorageDriver in Filament Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/filesystem-storage-driver Demonstrates how to register the FilesystemStorageDriver within a Filament panel provider and customize the storage disk and directory path. ```php use RalphJSmit\Filament\MediaLibrary\Drivers\FilesystemStorageDriver; // Basic registration $plugin->driver(FilesystemStorageDriver::class); // Configure specific disk $plugin->driver(FilesystemStorageDriver::class, function (FilesystemStorageDriver $driver) { return $driver->disk('s3'); }); // Configure disk and directory $plugin->driver(FilesystemStorageDriver::class, function (FilesystemStorageDriver $driver) { return $driver->disk('s3')->directory('media'); }); ``` -------------------------------- ### Define Relationship to MediaLibraryItem in Eloquent Model Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/drivers/drivers/01-media-library-item-driver Shows how to define a `BelongsTo` relationship in your Eloquent model to link to the `MediaLibraryItem` model. This allows for intuitive access to the media item's details through the defined relationship. ```php use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; class YourModel extends Model { public function featuredImage(): BelongsTo { return $this->belongsTo(MediaLibraryItem::class, 'featured_image_id'); } } ``` -------------------------------- ### Configure Static Scoped Folder in Filament Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/scoped-folder This snippet demonstrates how to register the Filament Media Library Pro plugin with a statically defined scoped folder using the `scopedFolder()` method. It utilizes `FileData` to specify the disk, path, and file type for the folder. ```php use RalphJSmit\Filament\Explore\Drivers\FilesystemStorageDriver\FileData; use RalphJSmit\Filament\Explore\Enums\FileType; $plugin->scopedFolder(FileData::fromDisk('s3', 'path/to/folder', FileType::Folder)) ``` -------------------------------- ### Add File Actions to Media Picker Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Demonstrates how to add custom actions to individual media items within the MediaPicker component using the `->fileActions()` method. This allows for specific operations like renaming or optimizing each file. ```php use RalphJSmit\Filament\Explore\Filament\Actions\RenameAction; use Filament\Actions\Action; use Filament\Support\Facades\Heroicon; use RalphJSmit\Filament\MediaLibrary\Models\MediaLibraryItem; MediaPicker::make('images') ->label('Choose images') ->multiple() ->fileActions([ RenameAction::make(), Action::make('optimize') ->icon(Heroicon::OutlinedSparkles) ->action(function (MediaLibraryItem $file) { // Perform an action... }), ]) ``` -------------------------------- ### Implement FilesystemStorageDriver in Filament Forms Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/drivers Shows the implementation of the FilesystemStorageDriver, which stores the file path directly in the model field instead of a database ID. ```php MediaPicker::make('featured_image_path') ->label('Featured image') ->required(); ``` -------------------------------- ### Enable Microsoft PowerPoint Uploads in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/accepted-file-types This snippet allows the upload of Microsoft PowerPoint presentations (.ppt and .pptx) to the Filament Media Library Pro using the `acceptMicrosoftPowerPoint()` method. This streamlines the handling of presentation files. ```php $plugin->acceptMicrosoftPowerPoint() ``` -------------------------------- ### Configure Custom Eloquent Models for Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/filament-media-library Enables the use of custom Eloquent models for media library items and folders. This configuration is done within the `modifyDriverUsing` callback by specifying the model classes for `mediaLibraryItemModel` and `mediaLibraryFolderModel`. ```php use RalphJSmit\Filament\MediaLibrary\Drivers\MediaLibraryItemDriver; $driver ->mediaLibraryItemModel(\App\Models\MediaLibraryItem::class) ->mediaLibraryFolderModel(\App\Models\MediaLibraryFolder::class); ``` -------------------------------- ### Load MediaLibraryItem via Relationship with MediaEntry Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-entry This snippet illustrates how to use the MediaEntry component when the media item is associated via a relationship. It utilizes the '->relationship()' method, specifying the relationship name to load the MediaLibraryItem directly. ```php MediaEntry::make('featuredImage') ->relationship() ->label('Featured image') ``` -------------------------------- ### Prepare TemporaryFileUploadData Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/programmatic-upload Creates a data object for file uploads either from an HTTP request's UploadedFile instance or from an existing file on a storage disk. ```php $uploadedFile = $request->file('item'); $temporaryFileUploadData = TemporaryFileUploadData::fromUploadedFile($uploadedFile); ``` ```php $temporaryFileUploadData = TemporaryFileUploadData::fromDisk( disk: 's3', path: 'path/to/file.jpg', shouldPreserveOriginalFile: true ); ``` -------------------------------- ### Implement MediaPicker in Filament Forms Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Basic implementation of the MediaPicker field within a Filament form. It supports both single and multiple file selection modes. ```php MediaPicker::make('image') ->label('Choose image') ->required(); MediaPicker::make('images') ->label('Choose images') ->multiple() ->required(); ``` -------------------------------- ### Update Custom Component Registration for Filament Media Library Pro V4 Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide This snippet demonstrates how custom page components (UploadMedia, BrowseLibrary, MediaInfo) were registered in V3. In V4, these methods are removed as the library is rebuilt with Filament Schemas. Review V4 documentation for equivalent features. ```php $plugin ->uploadMediaComponent("App\\Livewire\\MediaLibrary\\UploadMediaComponent::class") ->browseLibraryComponent("App\\Livewire\\MediaLibrary\\BrowseLibraryComponent::class") ->mediaInfoComponent("App\\Livewire\\MediaLibrary\\MediaInfoComponent::class") ``` -------------------------------- ### Configure Selection Constraints and Validation Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Methods to enforce file count limits and file type restrictions. Includes custom validation messaging for better user feedback. ```php MediaPicker::make('images') ->label('Select between 2-5 files for your header gallery') ->multiple() ->minFiles(2) ->maxFiles(5) ->validationMessages([ 'min' => 'Please select at least 2 brochures.', 'max' => 'Please select maximum 5 brochures.', ]); MediaPicker::make('documents') ->label('Choose documents') ->multiple() ->acceptedFileTypes(['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']) ->required(); ``` -------------------------------- ### Enable and Modify Media Conversions Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/filament-media-library Methods to enable media conversions and customize specific conversion settings like dimensions and formats within the Filament panel provider. ```php $driver->conversions(); use Spatie\MediaLibrary\Conversions\Conversion; $driver ->conversionResponsive(enabled: true, modifyUsing: function (Conversion $conversion) { return $conversion->keepOriginalImageFormat(); }) ->conversionMedium(enabled: true, width: 800) ->conversionSmall(enabled: true, width: 400) ->conversionThumb(enabled: true, width: 600, height: 600); ``` -------------------------------- ### Configure Additional Accepted File Types in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/accepted-file-types This snippet demonstrates how to configure the Filament Media Library Pro plugin to accept additional file types, such as JSON, plain text, and XML. This is done within the panel provider configuration. The library will display icons for file types it cannot generate previews for. ```php $plugin->additionalAcceptedFileTypes([ 'application/json', 'text/plain', 'application/xml', ]) ``` -------------------------------- ### Update Media Library URL Generator Configuration Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide If using a private disk with spatie/laravel-media-library, remove the custom 'url_generator' line from the config/media-library.php file and revert to the default. ```php /* * When urls to files get generated, this class will be called. Use the default * if your files are stored locally above the site root or on s3. */ 'url_generator' => RalphJSmit\Filament\MediaLibrary\UrlGenerator\MediaLibraryUrlGenerator::class, 'url_generator' => Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator::class, ``` -------------------------------- ### Configure MediaPicker for Eloquent Relationships Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Demonstrates how to bind the MediaPicker field to BelongsTo and BelongsToMany relationships. The component automatically handles record persistence, eliminating the need for manual save logic. ```php // BelongsTo relationship MediaPicker::make('featuredImage') ->label('Featured image') ->relationship('featuredImage') ->required(); // BelongsToMany relationship MediaPicker::make('images') ->label('Gallery images') ->relationship('images') ->multiple() ->required(); ``` -------------------------------- ### Registering modification closures in PanelProvider Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/modifications Demonstrates how to register a modification closure for a specific driver within the Filament panel provider. This approach uses the applyModificationUsing method to intercept and apply modifications to the target object. ```php $plugin ->driver(MediaLibraryItemDriver::class, function (MediaLibraryItemDriver $driver) { $driver->applyModificationUsing(function (mixed $target, Modification $modification, Closure $next) { if ($modification instanceof PublishedModification) { return $target->where('is_published', $modification->isPublished()); } return $next($target, $modification); }); }); ``` -------------------------------- ### Register Custom Driver in Filament Panel Provider (PHP) Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/custom-drivers Registers the custom media library driver with Filament. This is done within the `driver()` method of the Filament plugin configuration in your panel provider file, allowing Filament to use your custom implementation. ```php $plugin ->driver(CustomMediaLibraryDriver::class) ``` -------------------------------- ### Enable Audio Uploads in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/accepted-file-types This snippet enables the upload of various audio file types, including MP3, WAV, and FLAC, in the Filament Media Library Pro using the `acceptAudio()` method. This provides a convenient way to manage audio assets. ```php $plugin->acceptAudio() ``` -------------------------------- ### Enable ZIP Archive Uploads in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/accepted-file-types This snippet allows the upload of ZIP archives and other ZIP-compressed formats to the Filament Media Library Pro using the `acceptZip()` method. This is useful for managing compressed assets. ```php $plugin->acceptZip() ``` -------------------------------- ### Customize MediaEntry with ImageEntry Methods Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-entry This snippet demonstrates how to further customize the MediaEntry component by applying methods available on the ImageEntry class, such as 'circular()' and 'size()'. These methods allow for visual adjustments to the displayed media. ```php MediaEntry::make('thumbnail_id') ->label('Thumbnail') ->circular() ->size() ``` -------------------------------- ### Add Tooltip to Media Library Navigation Badge Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/media-library-page Provide a tooltip that appears when hovering over the navigation badge using the `->navigationBadgeTooltip()` method. This can offer more detailed information about the badge's meaning. ```php $plugin->navigationBadgeTooltip('Pro-customers only') ``` -------------------------------- ### Set Default Folder for MediaPicker Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/components/media-picker Specifies a default folder for the MediaPicker to open in, without restricting user navigation. This method accepts a MediaLibraryFolder object, a FileData object, or a closure for dynamic folder retrieval. Users can still navigate to other folders. ```php use RalphJSmit\Filament\Explore\Drivers\FilesystemStorageDriver\FileData; use RalphJSmit\Filament\Explore\Enums\FileType; MediaPicker::make('featured_image_path') ->defaultFolder(FileData::fromDisk('s3', 'path/to/folder', FileType::Folder)); ``` ```php MediaPicker::make('featured_image_path') ->defaultFolder(fn (Event $record) => FileData::fromDisk('s3', "events/{$record->getKey()}", FileType::Folder)); ``` -------------------------------- ### Update MediaColumn Component Configuration Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/guides/upgrade-guide Review instances of the `MediaColumn` in your tables. If `MediaColumn` is used to display media from a relationship, the `->relationship()` method must now be explicitly added. Dependencies include `RalphJSmit\Filament\MediaLibrary\Tables\Columns\MediaColumn`. ```php use RalphJSmit\Filament\MediaLibrary\Tables\Columns\MediaColumn; MediaColumn::make('images') ->relationship() ``` -------------------------------- ### Upload Files to Media Library Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/programmatic-upload Uses the driver instance to persist the prepared file data into the media library, optionally specifying a target folder. ```php $fileData = $driver->createFile( folder: null, temporaryFileUploadData: $temporaryFileUploadData, ); ``` -------------------------------- ### Implement a Filter using Modifications Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/advanced/modifications Demonstrates how to utilize a custom modification within a Filter class. The getModifications method processes input data and returns an array of modification instances to be applied by the driver. ```php use App\MediaLibrary\Modifications\Scopes\PublishedModification; use RalphJSmit\Filament\Explore\Filters\Filter; class PublishedFilter extends Filter { public function getModifications(array $data): array { $modifications = []; if (filled($isPublished = $data['is_published'] ?? null)) { $modifications[] = PublishedModification::make() ->published($isPublished); } return $modifications; } } ``` -------------------------------- ### Register Custom Media Conversions in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/drivers/filament-media-library Allows developers to register additional media conversion types for uploaded media. This is achieved using the `registerConversions()` method on the media library driver. Each new conversion consumes additional disk space. ```php $driver ->registerConversions(function (MediaLibraryItem $mediaLibraryItem, Media $media = null) { $mediaLibraryItem ->addMediaConversion('test'); // .. }) ``` -------------------------------- ### Hydrate File Info Edit Form with Existing Data in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/file-info This snippet shows how to populate the file info edit form with existing data using the `hydrateFileInfoEditUsing()` method. It defines a callback function that receives the file data and returns an array of values to pre-fill the form fields, ensuring data consistency. This is crucial for editing existing media items. ```php $driver ->hydrateFileInfoEditUsing(function (ConcernsFileData $file) { // Return an array with the field's values based on `$file`... return [ 'sku' => ... ]; }) ``` -------------------------------- ### Apply Custom Modification using MediaLibraryItemDriver Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/sorters This snippet shows how to apply a custom modification, specifically for featured items, using the MediaLibraryItemDriver. It defines how to order media based on the 'is_featured' column according to the sort direction provided by the modification. This requires the `MediaLibraryItemDriver` and `SortDirection` enum. ```php use App\MediaLibrary\Modifications\Orders\FeaturedModification; use RalphJSmit\Filament\Explore\Sorters\Enums\SortDirection; $plugin ->driver(MediaLibraryItemDriver::class, function (MediaLibraryItemDriver $driver) { $driver->applyModificationUsing(function (mixed $target, Modification $modification, Closure $next) { if ($modification instanceof FeaturedModification) { $direction = match ($modification->getSortDirection()) { SortDirection::Ascending => 'asc', SortDirection::Descending => 'desc', }; return $target->orderBy('is_featured', $direction); } return $next($target, $modification); }); }); ``` -------------------------------- ### Enable Microsoft Word Uploads in Filament Media Library Pro Source: https://filamentplugins.com/filament-media-library-pro/docs/v4/media-library/accepted-file-types This snippet enables the upload of Microsoft Word documents (.doc and .docx) to the Filament Media Library Pro via the `acceptMicrosoftWord()` method. This simplifies the management of Word files within the library. ```php $plugin->acceptMicrosoftWord() ```