Repeater and SpatieUpload Fill

Trying to do set a repeater field using
->afterStateUpdated(function (Get $get, Set $set) {
$this->participant = Participant::find($get('participant_id'));
$mediaItems = $this->participant->media;
$attachments = $mediaItems->map(function ($media) {
return [
'attachment' => $media,
'name' => $media->collection_name,
];
})->toArray();

ray($attachments)->label('Attachments');

$set('attachments', $attachments);
})
->afterStateUpdated(function (Get $get, Set $set) {
$this->participant = Participant::find($get('participant_id'));
$mediaItems = $this->participant->media;
$attachments = $mediaItems->map(function ($media) {
return [
'attachment' => $media,
'name' => $media->collection_name,
];
})->toArray();

ray($attachments)->label('Attachments');

$set('attachments', $attachments);
})
with the repeater having
->schema([
SpatieMediaLibraryFileUpload::make('attachment')
->previewable(false)
->reactive()
->required()
->hiddenLabel(),
TextInput::make('name')
->hiddenLabel()
->placeholder('File Name')
->reactive()
->required(),
]),
->schema([
SpatieMediaLibraryFileUpload::make('attachment')
->previewable(false)
->reactive()
->required()
->hiddenLabel(),
TextInput::make('name')
->hiddenLabel()
->placeholder('File Name')
->reactive()
->required(),
]),
. Im filling the name file, but getting
Filament\Forms\Components\SpatieMediaLibraryFileUpload::Filament\Forms\Components\{closure}(): Argument #2 ($file) must be of type string, array given, called in /#######/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
Filament\Forms\Components\SpatieMediaLibraryFileUpload::Filament\Forms\Components\{closure}(): Argument #2 ($file) must be of type string, array given, called in /#######/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
. Tried a few different ideas with media() on 'attachment', but im obviously missing something simple.
6 Replies
Mark Chaney
Mark ChaneyOP14mo ago
Anyone have any ideas on this? Im finding doing uploads and having a preview in my other element being a big pain since they arent saved yet
awcodes
awcodes14mo ago
I think it’s because you’re returning an array of arrays to the component instead of an array of paths to the file. Filepond can’t display an array for the image/preview.
Mark Chaney
Mark ChaneyOP14mo ago
@awcodes im still not sure what format filepond wants. It says it wants a string, but what string? the filename? thats not it
awcodes
awcodes14mo ago
Looks like it's just the path without the disk name, then that is converted to a UUID keyed array of the path:
$this->afterStateHydrated(static function (BaseFileUpload $component, string | array | null $state): void {
if (blank($state)) {
$component->state([]);

return;
}

$files = collect(Arr::wrap($state))
->filter(static function (string $file) use ($component): bool {
try {
return blank($file) || $component->getDisk()->exists($file);
} catch (UnableToCheckFileExistence $exception) {
return false;
}
})
->mapWithKeys(static fn (string $file): array => [((string) Str::uuid()) => $file])
->all();

$component->state($files);
});
$this->afterStateHydrated(static function (BaseFileUpload $component, string | array | null $state): void {
if (blank($state)) {
$component->state([]);

return;
}

$files = collect(Arr::wrap($state))
->filter(static function (string $file) use ($component): bool {
try {
return blank($file) || $component->getDisk()->exists($file);
} catch (UnableToCheckFileExistence $exception) {
return false;
}
})
->mapWithKeys(static fn (string $file): array => [((string) Str::uuid()) => $file])
->all();

$component->state($files);
});
so it looks like just an array of the filepath would work.
[
'media/1.jpg',
'media/2.jpg',
...
]
[
'media/1.jpg',
'media/2.jpg',
...
]
Mark Chaney
Mark ChaneyOP14mo ago
@awcodes im using s3. hmm
awcodes
awcodes14mo ago
shouldn't matter. i wouldn't think. the disk() on the fileupload will tell it to look in s3
Want results from more Discord servers?
Add your server