I have a Filepond component that works great for uploading images to a post.
However, I'm struggling with Filepond when needing to edit a post and its pre-exisiting images.
I want to load Filepond with the Post model's pre-existing images. The goal is to allow the user to upload, delete and reorder the images when editing the Post model, then update the database and the file system.
This is what I have so far:
<div
x-data=""
x-init="
// Plugins
FilePond.registerPlugin(FilePondPluginImagePreview);
FilePond.registerPlugin(FilePondPluginImageExifOrientation);
FilePond.registerPlugin(FilePondPluginFileValidateType);
FilePond.registerPlugin(FilePondPluginFileValidateSize);
FilePond.registerPlugin(FilePondPluginImageResize);
// Set options
FilePond.setOptions({
allowMultiple: true,
allowReorder: true,
itemInsertLocation: 'after',
server: {
process: (fieldName, file, metadata, load, error, progress, abort, transfer, options) => {
@this.upload('images', file, load, error, progress)
},
revert: (filename, load) => {
@this.removeUpload('images', filename, load)
},
},
// This allows me to reorder the images, essential resetting the $image variable in livewire component when ever th euser reorders the images in filepond
onreorderfiles(files, origin, target){
@this.set('images', null);
files.forEach(function(file) {
@this.upload('images', file.file);
});
},
});
// Create Filepond
const pond = FilePond.create($refs.input, {
acceptedFileTypes: ['image/png', 'image/jpeg'],
@if(optional($post)->images) // If we are editing a post and if that post has images
files: [
@foreach($post->images as $image)// Loop through each image for the post
{
// the server file reference
source: '{{ Storage::disk('post_images')->url($post->id . '/' . $image->filename.$image->extension) }}',
},
@endforeach
],
@endif
});
pond.on('addfile', (error, file) => {
if (error) {
console.log('Oh no');
return;
}
});
"
>
<div wire:ignore wire:key="images">
<input
id="image-upload"
type="file"
x-ref="input"
multiple
>
@error('images.*')
<p wire:key="error_images" class="mt-2 text-sm text-red-600" id="email-error">{{ $message }}</p>
@enderror
</div>
</div>
However this is causing all of the images to re-upload every time the user view the edit page.
There is a lot online on how to upload images using filepond and livewire, but not really much on how to edit.
Is there a better way to manage images, other than reuploading every time the user views the edit page?
Any help with this would greatly be appreciated.