### Install Filament Spatie Media Library Plugin Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Install the Spatie Media Library plugin for Filament using Composer. This command fetches the latest compatible version of the plugin. ```bash composer require filament/spatie-laravel-media-library-plugin:"^4.0" -W ``` -------------------------------- ### Specify Conversion for Spatie Media Library Table Column Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This example demonstrates how to specify a media conversion to be used when displaying an image in the `SpatieMediaLibraryImageColumn`. The `conversion()` method allows you to load a pre-generated thumbnail or other converted versions of the image. ```php use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;   SpatieMediaLibraryImageColumn::make('avatar') ->conversion('thumb') ``` -------------------------------- ### Specify Collection for Spatie Media Library Table Column Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This example demonstrates how to specify which media collection should be used for the `SpatieMediaLibraryImageColumn`. By calling `collection()` with the collection name, you can control which group of media is displayed. ```php use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;   SpatieMediaLibraryImageColumn::make('avatar') ->collection('avatars') ``` -------------------------------- ### Customize Media Name in Spatie Media Library Attachments Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This example demonstrates how to customize the media name for files uploaded via Spatie Media Library in the rich editor. It uses the `mediaName()` method, accepting a closure that generates a unique name based on random characters and the original filename. ```php use Filament\Forms\Components\RichEditor\FileAttachmentProviders\SpatieMediaLibraryFileAttachmentProvider; use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; use Illuminate\Support\Str;   SpatieMediaLibraryFileAttachmentProvider::make() ->mediaName(fn (TemporaryUploadedFile $file): string => Str::random() . '_' . $file->getClientOriginalName()) ``` -------------------------------- ### Run Database Migrations Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Execute pending database migrations to create the tables required by the Spatie Media Library. This includes the media table published in the previous step. ```bash php artisan migrate ``` -------------------------------- ### Enable Responsive Image Generation Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Automatically generate responsive image variants upon upload by calling the `responsiveImages()` method. This improves performance by serving appropriately sized images to different devices. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload;   SpatieMediaLibraryFileUpload::make('attachments') ->multiple() ->responsiveImages() ``` -------------------------------- ### Display Media from All Collections in Infolist Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Enables the `SpatieMediaLibraryImageEntry` to display media from all available collections, not just the default one. This is achieved using the `allCollections()` method. ```php use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; SpatieMediaLibraryImageEntry::make('avatar') ->allCollections() ``` -------------------------------- ### Publish Media Library Migrations Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Publish the necessary migration files for the Spatie Media Library to create the media table in your database. This command specifies the provider and the tag for the migrations. ```bash php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" ``` -------------------------------- ### Basic Spatie Media Library Infolist Entry Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Displays a media item in a Filament infolist using the `SpatieMediaLibraryImageEntry` component. This is the default configuration and will show the primary media associated with the entry. ```php use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; SpatieMediaLibraryImageEntry::make('avatar') ``` -------------------------------- ### Specify Image Conversion for Display Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Load a specific media conversion for displaying the file within the form using the `conversion()` method. This is helpful for showing thumbnails or other pre-processed versions of the uploaded media. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload;   SpatieMediaLibraryFileUpload::make('attachments') ->conversion('thumb') ``` -------------------------------- ### Configure Storage Disk for File Uploads Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Manually set the storage disk for file uploads using the `disk()` method. By default, files are uploaded to the disk defined in Filament's configuration, but this allows for custom configurations like 's3'. Note that Spatie's disk configuration is generally not used unless a disk is defined for a registered collection. ```php use Filament\Forms\Components\FileUpload; FileUpload::make('attachment') ->disk('s3') ``` -------------------------------- ### Specify Media Collection in Infolist Entry Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Configures the `SpatieMediaLibraryImageEntry` to display media from a specific collection. This is useful for organizing and displaying distinct sets of media within an infolist. By default, it shows media from the 'default' collection. ```php use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; SpatieMediaLibraryImageEntry::make('avatar') ->collection('avatars') ``` -------------------------------- ### Specify Conversion for Infolist Media Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Configures the `SpatieMediaLibraryImageEntry` to display a specific image conversion. This allows for showing optimized or different versions of the media in the infolist, controlled by the `conversion()` method. ```php use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; SpatieMediaLibraryImageEntry::make('avatar') ->conversion('thumb') ``` -------------------------------- ### Display All Collections in Spatie Media Library Table Column Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This code snippet shows how to configure the `SpatieMediaLibraryImageColumn` to display media from all available collections, not just the default one. This is achieved by calling the `allCollections()` method. ```php use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;   SpatieMediaLibraryImageColumn::make('avatar') ->allCollections() ``` -------------------------------- ### Enable File Reordering Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Allow users to reorder uploaded files through drag and drop functionality by using the `reorderable()` method. This feature enhances the user experience for managing multiple file uploads. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload; SpatieMediaLibraryFileUpload::make('attachments') ->multiple() ->reorderable() ``` -------------------------------- ### Apply Media-Specific Manipulations on Upload Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Apply custom image manipulations when files are uploaded using the `manipulations()` method. This method accepts an array where keys represent manipulation names (e.g., 'thumb') and values are arrays of manipulation options. This allows for pre-processing of images upon upload, such as resizing or applying effects. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload; SpatieMediaLibraryFileUpload::make('attachments') ->multiple() ->manipulations([ 'thumb' => ['orientation' => '90'], ]) ``` -------------------------------- ### Store Media Conversions on Separate Disk Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Configure the SpatieMediaLibraryFileUpload component to store media conversions on a different disk than the original file. This is achieved by passing the desired disk name to the `conversionsDisk()` method. This is useful for managing storage efficiently, for instance, storing original files locally and conversions on cloud storage. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload; SpatieMediaLibraryFileUpload::make('attachments') ->conversionsDisk('s3') ``` -------------------------------- ### Basic SpatieMediaLibraryFileUpload Usage Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Use the SpatieMediaLibraryFileUpload component in your Filament forms, similar to the standard file upload field. This component is designed to work with Spatie's Media Library package. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload; SpatieMediaLibraryFileUpload::make('avatar') ``` -------------------------------- ### Configure Rich Editor File Attachments with Spatie Media Library Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This snippet demonstrates how to register a rich content attribute on a model to use Spatie Media Library for file attachments in a rich editor. It requires calling `fileAttachmentProvider()` with `SpatieMediaLibraryFileAttachmentProvider::make()`. ```php use Filament\Forms\Components\RichEditor\FileAttachmentProviders\SpatieMediaLibraryFileAttachmentProvider; use Filament\Forms\Components\RichEditor\Models\Concerns\InteractsWithRichContent; use Filament\Forms\Components\RichEditor\Models\Contracts\HasRichContent; use Illuminate\Database\Eloquent\Model;   class Post extends Model implements HasRichContent { use InteractsWithRichContent;   public function setUpRichContent(): void { $this->registerRichContent('content') ->fileAttachmentProvider(SpatieMediaLibraryFileAttachmentProvider::make()); } } ``` -------------------------------- ### Add Custom Properties to Uploaded Files Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Attach custom metadata to uploaded files using the `customProperties()` method. This can be a static array or a dynamic callback function that receives the uploaded file and returns an array of properties. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload; SpatieMediaLibraryFileUpload::make('attachments') ->multiple() ->customProperties(['zip_filename_prefix' => 'folder/subfolder/']) ``` ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; use Spatie\Image\Image;   SpatieMediaLibraryFileUpload::make('image') ->image() ->customProperties(function (TemporaryUploadedFile $file): array { $image = Image::load($file->getRealPath());   return [ 'height' => $image->getHeight(), 'width' => $image->getWidth(), ]; }) ``` -------------------------------- ### Add Custom Headers to Uploaded Files Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Include custom HTTP headers with file uploads by utilizing the `customHeaders()` method. This is useful for setting specific directives like caching policies. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload;   SpatieMediaLibraryFileUpload::make('attachments') ->multiple() ->customHeaders(['CacheControl' => 'max-age=86400']) ``` -------------------------------- ### Display Spatie Media Library Image in Table Column Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This snippet shows how to use the `SpatieMediaLibraryImageColumn` to display an image from the Spatie Media Library in a Filament table. It's initialized with the name of the relationship or attribute holding the media. ```php use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;   SpatieMediaLibraryImageColumn::make('avatar') ``` -------------------------------- ### Add Custom Properties to Spatie Media Library Uploads Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This code shows how to pass custom properties to media files uploaded using Spatie Media Library in the rich editor. The `customProperties()` method allows you to define key-value pairs for additional metadata. ```php use Filament\Forms\Components\RichEditor\FileAttachmentProviders\SpatieMediaLibraryFileAttachmentProvider;   SpatieMediaLibraryFileAttachmentProvider::make() ->customProperties(['archived' => false]) ``` -------------------------------- ### Specify Media Collection Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Group uploaded files into specific collections using the `collection()` method. This allows for organized management of different types of media associated with a model. ```php use Filament\Forms\Components\SpatieMediaLibraryFileUpload; SpatieMediaLibraryFileUpload::make('avatar') ->collection('avatars') ``` -------------------------------- ### Filter Media in Infolist Entry Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Filters the media collection displayed in a Filament infolist entry using the `filterMediaUsing` method. This allows for displaying a subset of media based on custom properties, similar to table column filtering. It accepts a closure that manipulates the media collection. ```php use Filament\Tables\Columns\SpatieMediaLibraryImageEntry; // Note: This should likely be Filament\Infolists\Components\SpatieMediaLibraryImageEntry based on context use Illuminate\Support\Collection; SpatieMediaLibraryImageEntry::make('images') ->filterMediaUsing( fn (Collection $media): Collection => $media->where( 'custom_properties.gallery_id', 12345, ) ) ``` -------------------------------- ### Filter Media Collection Using Custom Properties Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Filter the media collection to only include files with specific custom properties using the `filterMediaUsing()` method. This method accepts a closure that receives the media collection and returns a filtered collection. This is useful for associating uploaded media with specific records or settings, ensuring data integrity and relevance. ```php use Filament\Schemas\Components\Utilities\Get; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Illuminate\Support\Collection; SpatieMediaLibraryFileUpload::make('images') ->customProperties(fn (Get $get): array => [ 'gallery_id' => $get('gallery_id'), ]) ->filterMediaUsing( fn (Collection $media, Get $get): Collection => $media->where( 'custom_properties.gallery_id', $get('gallery_id') ), ) ``` -------------------------------- ### Customize Spatie Media Library File Attachment Collection Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This code shows how to customize the media collection name used for file attachments in the rich editor by passing the desired collection name to the `collection()` method of the `SpatieMediaLibraryFileAttachmentProvider`. ```php use Filament\Forms\Components\RichEditor\FileAttachmentProviders\SpatieMediaLibraryFileAttachmentProvider; use Filament\Forms\Components\RichEditor\Models\Concerns\InteractsWithRichContent; use Filament\Forms\Components\RichEditor\Models\Contracts\HasRichContent; use Illuminate\Database\Eloquent\Model;   class Post extends Model implements HasRichContent { use InteractsWithRichContent;   public function setUpRichContent(): void { $this->registerRichContent('content') ->fileAttachmentProvider( SpatieMediaLibraryFileAttachmentProvider::make() ->collection('content-file-attachments') ); } } ``` -------------------------------- ### Filter Media in Table Column Source: https://filamentphp.com/plugins/filament-spatie-media-library/index Filters the media collection displayed in a Filament table column using the `filterMediaUsing` method. This allows for displaying a subset of media based on custom properties. It accepts a closure that manipulates the media collection. ```php use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; use Illuminate\Support\Collection; SpatieMediaLibraryImageColumn::make('images') ->filterMediaUsing( fn (Collection $media): Collection => $media->where( 'custom_properties.gallery_id', 12345, ) ) ``` -------------------------------- ### Preserve Filenames in Spatie Media Library Attachments Source: https://filamentphp.com/plugins/filament-spatie-media-library/index This code snippet illustrates how to preserve the original filenames of uploaded files when using Spatie Media Library for rich editor attachments by calling the `preserveFilenames()` method on the provider. ```php use Filament\Forms\Components\RichEditor\FileAttachmentProviders\SpatieMediaLibraryFileAttachmentProvider;   SpatieMediaLibraryFileAttachmentProvider::make() ->preserveFilenames() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.