79488646

Date: 2025-03-06 08:19:01
Score: 1.5
Natty:
Report link

In your ProductResource.php form() method, update the FileUpload field like this

FileUpload::make('image')
    ->label('Imagen')
    ->avatar()
    ->required()
    ->afterStateHydrated(function (FileUpload $component, $state, $record) {
        if ($record && $record->mainImage()) {
            $mainImage = $record->mainImage()->path;
            $component->state($mainImage); // Set the existing image path as the field value
        }
    })
    ->getUploadedFileNameForStorageUsing(function ($file) {
        return $file->store('products', 'public'); // Save the file to the public storage
    })
    ->directory('products') // Optional, for organized storage
    ->deleteUploadedFileUsing(function ($file) {
        \Storage::disk('public')->delete($file); // Delete the image from storage if replaced
    }),

how it would work

Optional: Validation If you want the field not to be required when editing, only when creating, you can add

->required(fn ($record) => $record === null)

Now,

let me know if it works for you?

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: Sameed Ediz