diff --git a/app/Filament/Resources/Entries/Schemas/EntryForm.php b/app/Filament/Resources/Entries/Schemas/EntryForm.php index 43cc948..02b5aee 100644 --- a/app/Filament/Resources/Entries/Schemas/EntryForm.php +++ b/app/Filament/Resources/Entries/Schemas/EntryForm.php @@ -50,21 +50,36 @@ class EntryForm ->allowHtml() ->options(function () { return Media::latest() - ->get() + ->limit(30) // Limit to 30 most recent items for performance + ->get(['id', 'file_name', 'name', 'uuid', 'collection_name', 'model_type', 'model_id', 'disk']) + ->filter(function (Media $item) { + // Only include media items that have a valid disk + return $item->disk !== null; + }) ->mapWithKeys(function (Media $item) { - $url = $item->getUrl(); - + try { + $url = $item->getUrl(); + } catch (\Exception $e) { + // Skip items that can't generate URLs + return []; + } + $fileName = e($item->file_name); $name = e($item->name ?? ''); - - $html = "
". - "\"{$fileName}\"/". - "{$name} — {$fileName}
"; + + // Smaller image preview for better performance + $html = "
" . + "{$fileName}" . + "
" . + "{$name}" . + "{$fileName}" . + "
"; return [$item->id => $html]; })->toArray(); }) ->searchable() + ->preload() ->required(), ]) ->action(function (array $data, SpatieMediaLibraryFileUpload $component) { @@ -82,16 +97,15 @@ class EntryForm // Clear existing featured image $record->clearMediaCollection('featured-image'); - // Download from the full URL and add as new media - $fullUrl = url($mediaItem->getUrl()); - - $newMedia = $record->addMediaFromUrl($fullUrl) - ->usingName($mediaItem->name ?: $mediaItem->file_name) - ->usingFileName($mediaItem->file_name) - ->toMediaCollection('featured-image', 'public'); + // Associate the existing media with this entry instead of copying + $mediaItem->update([ + 'model_type' => get_class($record), + 'model_id' => $record->id, + 'collection_name' => 'featured-image' + ]); // Update component state - $component->state([$newMedia->uuid]); + $component->state([$mediaItem->uuid]); \Filament\Notifications\Notification::make() ->success() @@ -116,24 +130,37 @@ class EntryForm ->label('Select an existing image') ->allowHtml() ->options(function () { - // We must 'get' the collection first so we can call getUrl() - // because 'url' is not a column in the Spatie database table. return Media::latest() - ->get() + ->limit(30) // Limit to 30 most recent items for performance + ->get(['id', 'file_name', 'name', 'uuid', 'collection_name', 'model_type', 'model_id', 'disk']) + ->filter(function (Media $item) { + // Only include media items that have a valid disk + return $item->disk !== null; + }) ->mapWithKeys(function (Media $item) { - $url = $item->getUrl(); - + try { + $url = $item->getUrl(); + } catch (\Exception $e) { + // Skip items that can't generate URLs + return []; + } + $fileName = e($item->file_name); $name = e($item->name ?? ''); - - $html = "
". - "\"{$fileName}\"/". - "{$name} — {$fileName}
"; + + // Smaller image preview for better performance + $html = "
" . + "{$fileName}" . + "
" . + "{$name}" . + "{$fileName}" . + "
"; return [$url => $html]; })->toArray(); }) ->searchable() + ->preload() ->required(), ]) ->action(function (array $data, RichEditor $component) {