diff --git a/app/Filament/Resources/Entries/Schemas/EntryForm.php b/app/Filament/Resources/Entries/Schemas/EntryForm.php index a0613d3..739fe50 100644 --- a/app/Filament/Resources/Entries/Schemas/EntryForm.php +++ b/app/Filament/Resources/Entries/Schemas/EntryForm.php @@ -11,6 +11,7 @@ use Filament\Forms\Components\Textarea; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Toggle; use Filament\Schemas\Schema; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Spatie\MediaLibrary\MediaCollections\Models\Media; @@ -67,22 +68,33 @@ class EntryForm ->action(function (array $data, SpatieMediaLibraryFileUpload $component) { $record = $component->getRecord(); - if ($record && $mediaItem = Media::find($data['image_id'])) { - // Clear any existing featured image + if (!$record) { + \Filament\Notifications\Notification::make() + ->warning() + ->title('Save the entry first') + ->send(); + return; + } + + if ($mediaItem = Media::find($data['image_id'])) { + // Clear existing featured image $record->clearMediaCollection('featured-image'); - // Get the full path to the media file - $fullPath = $mediaItem->getPath(); + // Download from the full URL and add as new media + $fullUrl = url($mediaItem->getUrl()); - // Add a copy of the media file to the featured-image collection - $record->addMedia($fullPath) + $newMedia = $record->addMediaFromUrl($fullUrl) ->usingName($mediaItem->name ?: $mediaItem->file_name) ->usingFileName($mediaItem->file_name) - ->preservingOriginal() ->toMediaCollection('featured-image'); - // Refresh the page to show the new featured image - $component->getLivewire()->dispatch('$refresh'); + // Update component state + $component->state([$newMedia->uuid]); + + \Filament\Notifications\Notification::make() + ->success() + ->title('Featured image set') + ->send(); } }) ),