Repeater with fileupload mixing state between them
I have a repeater which has a file upload and a text input field. However, when I have two or more repeater items, the file upload messes with the state. For example, if I add an image in repeater block 1 and save it, and then reload the page, the repeater block 2 will also have the same image.
return Tabs\Tab::make('Fotos')
->icon('heroicon-s-photo')
->schema([
Forms\Components\Repeater::make('grupo_fotos')
->label('')
->reorderable(false)
->collapsible()
->defaultItems(1)
->addActionLabel("Adicionar novo grupo de fotos")
->grid(2)
->schema([
SpatieMediaLibraryFileUpload::make('fotos')
->multiple()
->image()
->minFiles(1)
->maxFiles(4)
->helperText("Máximo de 4 imagens por grupo")
->panelLayout(null)
->previewable()
->columns(2)
->openable()
->customProperties(function (Get $get) {
return ['descricao'=>$get('descricao')];
})
->collection('photos'),
Forms\Components\TextInput::make('descricao')
->label('Descrição')
->required(),
]),
]);
3 Replies
I think the issue is the SpatieMediaLibrary part. Because that saves to a relationship and not a columns and therefore they all use the same.
🤔, makes sense, maybe if I customize the save I can fix it, I will try
Looking into the SpatieMediaLibraryFileUpload code, I can't see where this component is made "unique", with a key or something, and I can't know how to customize the database saving, so for now I'm not able to fix :/
Is possible to get the repeater index ? if I have it, i can use to build the collection name, which can fix that
It's "unique" insomuch as it's stored as a UUID, which is readable as a Media object. I'm not sure how to pass that to your repeater, but perhaps that gives you a direction to explore.