├── README.md ├── composer.json └── src ├── Forms └── Components │ └── SpatieMediaLibraryFileUpload.php ├── Infolists └── Components │ └── SpatieMediaLibraryImageEntry.php ├── SpatieLaravelMediaLibraryPlugin └── Collections │ └── AllMediaCollections.php ├── Support └── Concerns │ └── HasMediaFilter.php └── Tables └── Columns └── SpatieMediaLibraryImageColumn.php /README.md: -------------------------------------------------------------------------------- 1 | # Filament Spatie Media Library Plugin 2 | 3 | ## Installation 4 | 5 | Install the plugin with Composer: 6 | 7 | ```bash 8 | composer require filament/spatie-laravel-media-library-plugin:"^3.2" -W 9 | ``` 10 | 11 | If you haven't already done so, you need to publish the migration to create the media table: 12 | 13 | ```bash 14 | php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" 15 | ``` 16 | 17 | Run the migrations: 18 | 19 | ```bash 20 | php artisan migrate 21 | ``` 22 | 23 | You must also [prepare your Eloquent model](https://spatie.be/docs/laravel-medialibrary/basic-usage/preparing-your-model) for attaching media. 24 | 25 | > For more information, check out [Spatie's documentation](https://spatie.be/docs/laravel-medialibrary). 26 | 27 | ## Form component 28 | 29 | You may use the field in the same way as the [original file upload](https://filamentphp.com/docs/forms/fields/file-upload) field: 30 | 31 | ```php 32 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 33 | 34 | SpatieMediaLibraryFileUpload::make('avatar') 35 | ``` 36 | 37 | The media library file upload supports all the customization options of the [original file upload component](https://filamentphp.com/docs/forms/fields/file-upload). 38 | 39 | > The field will automatically load and save its uploads to your model. To set this functionality up, **you must also follow the instructions set out in the [setting a form model](https://filamentphp.com/docs/forms/adding-a-form-to-a-livewire-component#setting-a-form-model) section**. If you're using a [panel](../panels), you can skip this step. 40 | 41 | ### Passing a collection 42 | 43 | Optionally, you may pass a [`collection()`](https://spatie.be/docs/laravel-medialibrary/working-with-media-collections/simple-media-collections) allows you to group files into categories: 44 | 45 | ```php 46 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 47 | 48 | SpatieMediaLibraryFileUpload::make('avatar') 49 | ->collection('avatars') 50 | ``` 51 | 52 | ### Configuring the storage disk and directory 53 | 54 | By default, files will be uploaded publicly to your storage disk defined in the [Filament configuration file](https://filamentphp.com/docs/forms/installation#publishing-configuration). You can also set the `FILAMENT_FILESYSTEM_DISK` environment variable to change this. This is to ensure consistency between all Filament packages. Spatie's disk configuration will not be used, unless you [define a disk for a registered collection](https://spatie.be/docs/laravel-medialibrary/working-with-media-collections/defining-media-collections#content-using-a-specific-disk). 55 | 56 | Alternatively, you can manually set the disk with the `disk()` method: 57 | 58 | ```php 59 | use Filament\Forms\Components\FileUpload; 60 | 61 | FileUpload::make('attachment') 62 | ->disk('s3') 63 | ``` 64 | 65 | The base file upload component also has configuration options for setting the `directory()` and `visibility()` of uploaded files. These are not used by the media library file upload component. Spatie's package has its own system for determining the directory of a newly-uploaded file, and it does not support uploading private files out of the box. One way to store files privately is to configure this in your S3 bucket settings, in which case you should also use `visibility('private')` to ensure that Filament generates temporary URLs for your files. 66 | 67 | ### Reordering files 68 | 69 | In addition to the behavior of the normal file upload, Spatie's Media Library also allows users to reorder files. 70 | 71 | To enable this behavior, use the `reorderable()` method: 72 | 73 | ```php 74 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 75 | 76 | SpatieMediaLibraryFileUpload::make('attachments') 77 | ->multiple() 78 | ->reorderable() 79 | ``` 80 | 81 | You may now drag and drop files into order. 82 | 83 | ### Adding custom properties 84 | 85 | You may pass in [custom properties](https://spatie.be/docs/laravel-medialibrary/advanced-usage/using-custom-properties) when uploading files using the `customProperties()` method: 86 | 87 | ```php 88 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 89 | 90 | SpatieMediaLibraryFileUpload::make('attachments') 91 | ->multiple() 92 | ->customProperties(['zip_filename_prefix' => 'folder/subfolder/']) 93 | ``` 94 | 95 | ### Adding custom headers 96 | 97 | You may pass in custom headers when uploading files using the `customHeaders()` method: 98 | 99 | ```php 100 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 101 | 102 | SpatieMediaLibraryFileUpload::make('attachments') 103 | ->multiple() 104 | ->customHeaders(['CacheControl' => 'max-age=86400']) 105 | ``` 106 | 107 | ### Generating responsive images 108 | 109 | You may [generate responsive images](https://spatie.be/docs/laravel-medialibrary/responsive-images/getting-started-with-responsive-images) when the files are uploaded using the `responsiveImages()` method: 110 | 111 | ```php 112 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 113 | 114 | SpatieMediaLibraryFileUpload::make('attachments') 115 | ->multiple() 116 | ->responsiveImages() 117 | ``` 118 | 119 | ### Using conversions 120 | 121 | You may also specify a `conversion()` to load the file from showing it in the form, if present: 122 | 123 | ```php 124 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 125 | 126 | SpatieMediaLibraryFileUpload::make('attachments') 127 | ->conversion('thumb') 128 | ``` 129 | 130 | #### Storing conversions on a separate disk 131 | 132 | You can store your conversions and responsive images on a disk other than the one where you save the original file. Pass the name of the disk where you want conversion to be saved to the `conversionsDisk()` method: 133 | 134 | ```php 135 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 136 | 137 | SpatieMediaLibraryFileUpload::make('attachments') 138 | ->conversionsDisk('s3') 139 | ``` 140 | 141 | ### Storing media-specific manipulations 142 | 143 | You may pass in [manipulations](https://spatie.be/docs/laravel-medialibrary/advanced-usage/storing-media-specific-manipulations#breadcrumb) that are run when files are uploaded using the `manipulations()` method: 144 | 145 | ```php 146 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 147 | 148 | SpatieMediaLibraryFileUpload::make('attachments') 149 | ->multiple() 150 | ->manipulations([ 151 | 'thumb' => ['orientation' => '90'], 152 | ]) 153 | ``` 154 | 155 | ### Filtering media 156 | 157 | It's possible to target a file upload component to only handle a certain subset of media in a collection. To do that, you can filter the media collection using the `filterMediaUsing()` method. This method accepts a function that receives the `$media` collection and manipulates it. You can use any [collection method](https://laravel.com/docs/collections#available-methods) to filter it. 158 | 159 | For example, you could scope the field to only handle media that has certain custom properties: 160 | 161 | ```php 162 | use Filament\Forms\Components\SpatieMediaLibraryFileUpload; 163 | use Filament\Forms\Get; 164 | use Illuminate\Support\Collection; 165 | 166 | SpatieMediaLibraryFileUpload::make('images') 167 | ->customProperties(fn (Get $get): array => [ 168 | 'gallery_id' => $get('gallery_id'), 169 | ]) 170 | ->filterMediaUsing( 171 | fn (Collection $media, Get $get): Collection => $media->where( 172 | 'custom_properties.gallery_id', 173 | $get('gallery_id') 174 | ), 175 | ) 176 | ``` 177 | 178 | ## Table column 179 | 180 | To use the media library image column: 181 | 182 | ```php 183 | use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; 184 | 185 | SpatieMediaLibraryImageColumn::make('avatar') 186 | ``` 187 | 188 | The media library image column supports all the customization options of the [original image column](https://filamentphp.com/docs/tables/columns/image). 189 | 190 | ### Passing a collection 191 | 192 | Optionally, you may pass a `collection()`: 193 | 194 | ```php 195 | use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; 196 | 197 | SpatieMediaLibraryImageColumn::make('avatar') 198 | ->collection('avatars') 199 | ``` 200 | 201 | The [collection](https://spatie.be/docs/laravel-medialibrary/working-with-media-collections/simple-media-collections) allows you to group files into categories. 202 | 203 | By default, only media without a collection (using the `default` collection) will be shown. If you want to show media from all collections, you can use the `allCollections()` method: 204 | 205 | ```php 206 | use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; 207 | 208 | SpatieMediaLibraryImageColumn::make('avatar') 209 | ->allCollections() 210 | ``` 211 | 212 | ### Using conversions 213 | 214 | You may also specify a `conversion()` to load the file from showing it in the table, if present: 215 | 216 | ```php 217 | use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; 218 | 219 | SpatieMediaLibraryImageColumn::make('avatar') 220 | ->conversion('thumb') 221 | ``` 222 | 223 | ### Filtering media 224 | 225 | It's possible to target the column to only display a subset of media in a collection. To do that, you can filter the media collection using the `filterMediaUsing()` method. This method accepts a function that receives the `$media` collection and manipulates it. You can use any [collection method](https://laravel.com/docs/collections#available-methods) to filter it. 226 | 227 | For example, you could scope the column to only display media that has certain custom properties: 228 | 229 | ```php 230 | use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; 231 | use Illuminate\Support\Collection; 232 | 233 | SpatieMediaLibraryImageColumn::make('images') 234 | ->filterMediaUsing( 235 | fn (Collection $media): Collection => $media->where( 236 | 'custom_properties.gallery_id', 237 | 12345, 238 | ), 239 | ) 240 | ``` 241 | 242 | ## Infolist entry 243 | 244 | To use the media library image entry: 245 | 246 | ```php 247 | use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; 248 | 249 | SpatieMediaLibraryImageEntry::make('avatar') 250 | ``` 251 | 252 | The media library image entry supports all the customization options of the [original image entry](https://filamentphp.com/docs/infolists/entries/image). 253 | 254 | ### Passing a collection 255 | 256 | Optionally, you may pass a `collection()`: 257 | 258 | ```php 259 | use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; 260 | 261 | SpatieMediaLibraryImageEntry::make('avatar') 262 | ->collection('avatars') 263 | ``` 264 | 265 | The [collection](https://spatie.be/docs/laravel-medialibrary/working-with-media-collections/simple-media-collections) allows you to group files into categories. 266 | 267 | By default, only media without a collection (using the `default` collection) will be shown. If you want to show media from all collections, you can use the `allCollections()` method: 268 | 269 | ```php 270 | use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; 271 | 272 | SpatieMediaLibraryImageEntry::make('avatar') 273 | ->allCollections() 274 | ``` 275 | 276 | ### Using conversions 277 | 278 | You may also specify a `conversion()` to load the file from showing it in the infolist, if present: 279 | 280 | ```php 281 | use Filament\Infolists\Components\SpatieMediaLibraryImageEntry; 282 | 283 | SpatieMediaLibraryImageEntry::make('avatar') 284 | ->conversion('thumb') 285 | ``` 286 | 287 | ### Filtering media 288 | 289 | It's possible to target the entry to only display a subset of media in a collection. To do that, you can filter the media collection using the `filterMediaUsing()` method. This method accepts a function that receives the `$media` collection and manipulates it. You can use any [collection method](https://laravel.com/docs/collections#available-methods) to filter it. 290 | 291 | For example, you could scope the entry to only display media that has certain custom properties: 292 | 293 | ```php 294 | use Filament\Tables\Columns\SpatieMediaLibraryImageEntry; 295 | use Illuminate\Support\Collection; 296 | 297 | SpatieMediaLibraryImageEntry::make('images') 298 | ->filterMediaUsing( 299 | fn (Collection $media): Collection => $media->where( 300 | 'custom_properties.gallery_id', 301 | 12345, 302 | ), 303 | ) 304 | ``` 305 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "filament/spatie-laravel-media-library-plugin", 3 | "description": "Filament support for `spatie/laravel-medialibrary`.", 4 | "license": "MIT", 5 | "homepage": "https://github.com/filamentphp/filament", 6 | "support": { 7 | "issues": "https://github.com/filamentphp/filament/issues", 8 | "source": "https://github.com/filamentphp/filament" 9 | }, 10 | "require": { 11 | "php": "^8.1", 12 | "illuminate/support": "^10.45|^11.0|^12.0", 13 | "spatie/laravel-medialibrary": "^10.0|^11.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Filament\\": "src" 18 | } 19 | }, 20 | "config": { 21 | "sort-packages": true 22 | }, 23 | "minimum-stability": "dev", 24 | "prefer-stable": true 25 | } 26 | -------------------------------------------------------------------------------- /src/Forms/Components/SpatieMediaLibraryFileUpload.php: -------------------------------------------------------------------------------- 1 | | Closure | null 33 | */ 34 | protected array | Closure | null $customHeaders = null; 35 | 36 | /** 37 | * @var array | Closure | null 38 | */ 39 | protected array | Closure | null $customProperties = null; 40 | 41 | /** 42 | * @var array> | Closure | null 43 | */ 44 | protected array | Closure | null $manipulations = null; 45 | 46 | /** 47 | * @var array | Closure | null 48 | */ 49 | protected array | Closure | null $properties = null; 50 | 51 | protected function setUp(): void 52 | { 53 | parent::setUp(); 54 | 55 | $this->loadStateFromRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component, HasMedia $record): void { 56 | /** @var Model&HasMedia $record */ 57 | $media = $record->load('media')->getMedia($component->getCollection() ?? 'default') 58 | ->when( 59 | $component->hasMediaFilter(), 60 | fn (Collection $media) => $component->filterMedia($media) 61 | ) 62 | ->when( 63 | ! $component->isMultiple(), 64 | fn (Collection $media): Collection => $media->take(1), 65 | ) 66 | ->mapWithKeys(function (Media $media): array { 67 | $uuid = $media->getAttributeValue('uuid'); 68 | 69 | return [$uuid => $uuid]; 70 | }) 71 | ->toArray(); 72 | 73 | $component->state($media); 74 | }); 75 | 76 | $this->afterStateHydrated(static function (BaseFileUpload $component, string | array | null $state): void { 77 | if (is_array($state)) { 78 | return; 79 | } 80 | 81 | $component->state([]); 82 | }); 83 | 84 | $this->beforeStateDehydrated(null); 85 | 86 | $this->dehydrated(false); 87 | 88 | $this->getUploadedFileUsing(static function (SpatieMediaLibraryFileUpload $component, string $file): ?array { 89 | if (! $component->getRecord()) { 90 | return null; 91 | } 92 | 93 | /** @var ?Media $media */ 94 | $media = $component->getRecord()->getRelationValue('media')->firstWhere('uuid', $file); 95 | 96 | $url = null; 97 | 98 | if ($component->getVisibility() === 'private') { 99 | $conversion = $component->getConversion(); 100 | 101 | try { 102 | $url = $media?->getTemporaryUrl( 103 | now()->addMinutes(5), 104 | (filled($conversion) && $media->hasGeneratedConversion($conversion)) ? $conversion : '', 105 | ); 106 | } catch (Throwable $exception) { 107 | // This driver does not support creating temporary URLs. 108 | } 109 | } 110 | 111 | if ($component->getConversion() && $media?->hasGeneratedConversion($component->getConversion())) { 112 | $url ??= $media->getUrl($component->getConversion()); 113 | } 114 | 115 | $url ??= $media?->getUrl(); 116 | 117 | return [ 118 | 'name' => $media?->getAttributeValue('name') ?? $media?->getAttributeValue('file_name'), 119 | 'size' => $media?->getAttributeValue('size'), 120 | 'type' => $media?->getAttributeValue('mime_type'), 121 | 'url' => $url, 122 | ]; 123 | }); 124 | 125 | $this->saveRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component) { 126 | $component->deleteAbandonedFiles(); 127 | $component->saveUploadedFiles(); 128 | }); 129 | 130 | $this->saveUploadedFileUsing(static function (SpatieMediaLibraryFileUpload $component, TemporaryUploadedFile $file, ?Model $record): ?string { 131 | if (! method_exists($record, 'addMediaFromString')) { 132 | return $file; 133 | } 134 | 135 | try { 136 | if (! $file->exists()) { 137 | return null; 138 | } 139 | } catch (UnableToCheckFileExistence $exception) { 140 | return null; 141 | } 142 | 143 | /** @var FileAdder $mediaAdder */ 144 | $mediaAdder = $record->addMediaFromString($file->get()); 145 | 146 | $filename = $component->getUploadedFileNameForStorage($file); 147 | 148 | $media = $mediaAdder 149 | ->addCustomHeaders($component->getCustomHeaders()) 150 | ->usingFileName($filename) 151 | ->usingName($component->getMediaName($file) ?? pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) 152 | ->storingConversionsOnDisk($component->getConversionsDisk() ?? '') 153 | ->withCustomProperties($component->getCustomProperties()) 154 | ->withManipulations($component->getManipulations()) 155 | ->withResponsiveImagesIf($component->hasResponsiveImages()) 156 | ->withProperties($component->getProperties()) 157 | ->toMediaCollection($component->getCollection() ?? 'default', $component->getDiskName()); 158 | 159 | return $media->getAttributeValue('uuid'); 160 | }); 161 | 162 | $this->reorderUploadedFilesUsing(static function (SpatieMediaLibraryFileUpload $component, ?Model $record, array $state): array { 163 | $uuids = array_filter(array_values($state)); 164 | 165 | $mediaClass = ($record && method_exists($record, 'getMediaModel')) ? $record->getMediaModel() : null; 166 | $mediaClass ??= config('media-library.media_model', Media::class); 167 | 168 | $mappedIds = $mediaClass::query()->whereIn('uuid', $uuids)->pluck(app($mediaClass)->getKeyName(), 'uuid')->toArray(); 169 | 170 | $mediaClass::setNewOrder([ 171 | ...array_flip($uuids), 172 | ...$mappedIds, 173 | ]); 174 | 175 | return $state; 176 | }); 177 | } 178 | 179 | public function collection(string | Closure | null $collection): static 180 | { 181 | $this->collection = $collection; 182 | 183 | return $this; 184 | } 185 | 186 | public function conversion(string | Closure | null $conversion): static 187 | { 188 | $this->conversion = $conversion; 189 | 190 | return $this; 191 | } 192 | 193 | public function conversionsDisk(string | Closure | null $disk): static 194 | { 195 | $this->conversionsDisk = $disk; 196 | 197 | return $this; 198 | } 199 | 200 | /** 201 | * @param array | Closure | null $headers 202 | */ 203 | public function customHeaders(array | Closure | null $headers): static 204 | { 205 | $this->customHeaders = $headers; 206 | 207 | return $this; 208 | } 209 | 210 | /** 211 | * @param array | Closure | null $properties 212 | */ 213 | public function customProperties(array | Closure | null $properties): static 214 | { 215 | $this->customProperties = $properties; 216 | 217 | return $this; 218 | } 219 | 220 | /** 221 | * @param array> | Closure | null $manipulations 222 | */ 223 | public function manipulations(array | Closure | null $manipulations): static 224 | { 225 | $this->manipulations = $manipulations; 226 | 227 | return $this; 228 | } 229 | 230 | /** 231 | * @param array | Closure | null $properties 232 | */ 233 | public function properties(array | Closure | null $properties): static 234 | { 235 | $this->properties = $properties; 236 | 237 | return $this; 238 | } 239 | 240 | public function responsiveImages(bool | Closure $condition = true): static 241 | { 242 | $this->hasResponsiveImages = $condition; 243 | 244 | return $this; 245 | } 246 | 247 | public function deleteAbandonedFiles(): void 248 | { 249 | /** @var Model&HasMedia $record */ 250 | $record = $this->getRecord(); 251 | 252 | $record 253 | ->getMedia($this->getCollection() ?? 'default') 254 | ->whereNotIn('uuid', array_keys($this->getState() ?? [])) 255 | ->when($this->hasMediaFilter(), fn (Collection $media): Collection => $this->filterMedia($media)) 256 | ->each(fn (Media $media) => $media->delete()); 257 | } 258 | 259 | public function getDiskName(): string 260 | { 261 | if ($diskName = $this->evaluate($this->diskName)) { 262 | return $diskName; 263 | } 264 | 265 | /** @var Model&HasMedia $model */ 266 | $model = $this->getModelInstance(); 267 | 268 | $collection = $this->getCollection() ?? 'default'; 269 | 270 | /** @phpstan-ignore-next-line */ 271 | $diskNameFromRegisteredConversions = $model 272 | ->getRegisteredMediaCollections() 273 | ->filter(fn (MediaCollection $mediaCollection): bool => $mediaCollection->name === $collection) 274 | ->first() 275 | ?->diskName; 276 | 277 | return $diskNameFromRegisteredConversions ?? config('filament.default_filesystem_disk'); 278 | } 279 | 280 | public function getCollection(): ?string 281 | { 282 | return $this->evaluate($this->collection); 283 | } 284 | 285 | public function getConversion(): ?string 286 | { 287 | return $this->evaluate($this->conversion); 288 | } 289 | 290 | public function getConversionsDisk(): ?string 291 | { 292 | return $this->evaluate($this->conversionsDisk); 293 | } 294 | 295 | /** 296 | * @return array 297 | */ 298 | public function getCustomHeaders(): array 299 | { 300 | return $this->evaluate($this->customHeaders) ?? []; 301 | } 302 | 303 | /** 304 | * @return array 305 | */ 306 | public function getCustomProperties(): array 307 | { 308 | return $this->evaluate($this->customProperties) ?? []; 309 | } 310 | 311 | /** 312 | * @return array> 313 | */ 314 | public function getManipulations(): array 315 | { 316 | return $this->evaluate($this->manipulations) ?? []; 317 | } 318 | 319 | /** 320 | * @return array 321 | */ 322 | public function getProperties(): array 323 | { 324 | return $this->evaluate($this->properties) ?? []; 325 | } 326 | 327 | public function hasResponsiveImages(): bool 328 | { 329 | return (bool) $this->evaluate($this->hasResponsiveImages); 330 | } 331 | 332 | public function mediaName(string | Closure | null $name): static 333 | { 334 | $this->mediaName = $name; 335 | 336 | return $this; 337 | } 338 | 339 | public function getMediaName(TemporaryUploadedFile $file): ?string 340 | { 341 | return $this->evaluate($this->mediaName, [ 342 | 'file' => $file, 343 | ]); 344 | } 345 | } 346 | -------------------------------------------------------------------------------- /src/Infolists/Components/SpatieMediaLibraryImageEntry.php: -------------------------------------------------------------------------------- 1 | defaultImageUrl(function (SpatieMediaLibraryImageEntry $component, Model $record): ?string { 28 | if ($component->hasRelationship($record)) { 29 | $record = $component->getRelationshipResults($record); 30 | } 31 | 32 | $records = Arr::wrap($record); 33 | 34 | $collection = $component->getCollection(); 35 | 36 | if (! is_string($collection)) { 37 | $collection = 'default'; 38 | } 39 | 40 | foreach ($records as $record) { 41 | $url = $record->getFallbackMediaUrl($collection, $component->getConversion() ?? ''); 42 | 43 | if (blank($url)) { 44 | continue; 45 | } 46 | 47 | return $url; 48 | } 49 | 50 | return null; 51 | }); 52 | } 53 | 54 | public function collection(string | AllMediaCollections | Closure | null $collection): static 55 | { 56 | $this->collection = $collection; 57 | 58 | return $this; 59 | } 60 | 61 | public function allCollections(): static 62 | { 63 | $this->collection(AllMediaCollections::make()); 64 | 65 | return $this; 66 | } 67 | 68 | public function conversion(string | Closure | null $conversion): static 69 | { 70 | $this->conversion = $conversion; 71 | 72 | return $this; 73 | } 74 | 75 | public function getCollection(): string | AllMediaCollections | null 76 | { 77 | return $this->evaluate($this->collection); 78 | } 79 | 80 | public function getConversion(): ?string 81 | { 82 | return $this->evaluate($this->conversion); 83 | } 84 | 85 | public function getImageUrl(?string $state = null): ?string 86 | { 87 | $record = $this->getRecord(); 88 | 89 | if (! $record) { 90 | return null; 91 | } 92 | 93 | if ($this->hasRelationship($record)) { 94 | $record = $this->getRelationshipResults($record); 95 | } 96 | 97 | $records = Arr::wrap($record); 98 | 99 | foreach ($records as $record) { 100 | /** @var Model $record */ 101 | 102 | /** @var ?Media $media */ 103 | $media = $record->getRelationValue('media')->first(fn (Media $media): bool => $media->uuid === $state); 104 | 105 | if (! $media) { 106 | continue; 107 | } 108 | 109 | $conversion = $this->getConversion(); 110 | 111 | if ($this->getVisibility() === 'private') { 112 | try { 113 | return $media->getTemporaryUrl( 114 | now()->addMinutes(5), 115 | $conversion ?? '', 116 | ); 117 | } catch (Throwable $exception) { 118 | // This driver does not support creating temporary URLs. 119 | } 120 | } 121 | 122 | return $media->getAvailableUrl(Arr::wrap($conversion)); 123 | } 124 | 125 | return null; 126 | } 127 | 128 | /** 129 | * @return array 130 | */ 131 | public function getState(): array 132 | { 133 | $record = $this->getRecord(); 134 | 135 | if ($this->hasRelationship($record)) { 136 | $record = $this->getRelationshipResults($record); 137 | } 138 | 139 | $records = Arr::wrap($record); 140 | 141 | $state = []; 142 | 143 | $collection = $this->getCollection() ?? 'default'; 144 | 145 | foreach ($records as $record) { 146 | /** @var Model $record */ 147 | $state = [ 148 | ...$state, 149 | ...$record->getRelationValue('media') 150 | ->when( 151 | ! $collection instanceof AllMediaCollections, 152 | fn (MediaCollection $mediaCollection) => $mediaCollection->filter(fn (Media $media): bool => $media->getAttributeValue('collection_name') === $collection), 153 | ) 154 | ->when( 155 | $this->hasMediaFilter(), 156 | fn (Collection $media) => $this->filterMedia($media) 157 | ) 158 | ->sortBy('order_column') 159 | ->pluck('uuid') 160 | ->all(), 161 | ]; 162 | } 163 | 164 | return array_unique($state); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/SpatieLaravelMediaLibraryPlugin/Collections/AllMediaCollections.php: -------------------------------------------------------------------------------- 1 | filterMediaUsing = $callback; 15 | 16 | return $this; 17 | } 18 | 19 | public function filterMedia(Collection $media): Collection 20 | { 21 | return $this->evaluate($this->filterMediaUsing, [ 22 | 'media' => $media, 23 | ]) ?? $media; 24 | } 25 | 26 | public function hasMediaFilter(): bool 27 | { 28 | return $this->filterMediaUsing instanceof Closure; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Tables/Columns/SpatieMediaLibraryImageColumn.php: -------------------------------------------------------------------------------- 1 | defaultImageUrl(function (SpatieMediaLibraryImageColumn $column, Model $record): ?string { 30 | if ($column->hasRelationship($record)) { 31 | $record = $column->getRelationshipResults($record); 32 | } 33 | 34 | $records = Arr::wrap($record); 35 | 36 | $collection = $column->getCollection(); 37 | 38 | if (! is_string($collection)) { 39 | $collection = 'default'; 40 | } 41 | 42 | foreach ($records as $record) { 43 | $url = $record->getFallbackMediaUrl($collection, $column->getConversion() ?? ''); 44 | 45 | if (blank($url)) { 46 | continue; 47 | } 48 | 49 | return $url; 50 | } 51 | 52 | return null; 53 | }); 54 | } 55 | 56 | public function collection(string | AllMediaCollections | Closure | null $collection): static 57 | { 58 | $this->collection = $collection; 59 | 60 | return $this; 61 | } 62 | 63 | public function allCollections(): static 64 | { 65 | $this->collection(AllMediaCollections::make()); 66 | 67 | return $this; 68 | } 69 | 70 | public function conversion(string | Closure | null $conversion): static 71 | { 72 | $this->conversion = $conversion; 73 | 74 | return $this; 75 | } 76 | 77 | public function getCollection(): string | AllMediaCollections | null 78 | { 79 | return $this->evaluate($this->collection); 80 | } 81 | 82 | public function getConversion(): ?string 83 | { 84 | return $this->evaluate($this->conversion); 85 | } 86 | 87 | public function getImageUrl(?string $state = null): ?string 88 | { 89 | $record = $this->getRecord(); 90 | 91 | if ($this->hasRelationship($record)) { 92 | $record = $this->getRelationshipResults($record); 93 | } 94 | 95 | $records = Arr::wrap($record); 96 | 97 | foreach ($records as $record) { 98 | /** @var Model $record */ 99 | 100 | /** @var ?Media $media */ 101 | $media = $record->getRelationValue('media')->first(fn (Media $media): bool => $media->uuid === $state); 102 | 103 | if (! $media) { 104 | continue; 105 | } 106 | 107 | $conversion = $this->getConversion(); 108 | 109 | if ($this->getVisibility() === 'private') { 110 | try { 111 | return $media->getTemporaryUrl( 112 | now()->addMinutes(5), 113 | $conversion ?? '', 114 | ); 115 | } catch (Throwable $exception) { 116 | // This driver does not support creating temporary URLs. 117 | } 118 | } 119 | 120 | return $media->getAvailableUrl(Arr::wrap($conversion)); 121 | } 122 | 123 | return null; 124 | } 125 | 126 | /** 127 | * @return array 128 | */ 129 | public function getState(): array 130 | { 131 | $record = $this->getRecord(); 132 | 133 | if ($this->hasRelationship($record)) { 134 | $record = $this->getRelationshipResults($record); 135 | } 136 | 137 | $records = Arr::wrap($record); 138 | 139 | $state = []; 140 | 141 | $collection = $this->getCollection() ?? 'default'; 142 | 143 | foreach ($records as $record) { 144 | /** @var Model $record */ 145 | $state = [ 146 | ...$state, 147 | ...$record->getRelationValue('media') 148 | ->when( 149 | ! $collection instanceof AllMediaCollections, 150 | fn (MediaCollection $mediaCollection) => $mediaCollection->filter(fn (Media $media): bool => $media->getAttributeValue('collection_name') === $collection), 151 | ) 152 | ->when( 153 | $this->hasMediaFilter(), 154 | fn (Collection $media) => $this->filterMedia($media) 155 | ) 156 | ->sortBy('order_column') 157 | ->pluck('uuid') 158 | ->all(), 159 | ]; 160 | } 161 | 162 | return array_unique($state); 163 | } 164 | 165 | public function applyEagerLoading(Builder | Relation $query): Builder | Relation 166 | { 167 | if ($this->isHidden()) { 168 | return $query; 169 | } 170 | 171 | /** @phpstan-ignore-next-line */ 172 | $modifyMediaQuery = fn (Builder | Relation $query) => $query->ordered(); 173 | 174 | if ($this->hasRelationship($query->getModel())) { 175 | return $query->with([ 176 | "{$this->getRelationshipName()}.media" => $modifyMediaQuery, 177 | ]); 178 | } 179 | 180 | return $query->with(['media' => $modifyMediaQuery]); 181 | } 182 | } 183 | --------------------------------------------------------------------------------