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?