Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brentxscholl's avatar

Handling pre-existing images when editing a model using Livewire & Filepond

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.

0 likes
3 replies
tykus's avatar

The existing files in the files key should be an array of objects, each object being in the form:

{
  "source":55, // id on server
  "options":{
    "type":"local",
    "file":{
      "name":"",  // url to the file
            "size": 0, // the file size in bytes
            "type":"" // mimetype
         },
         "metadata":{
            "poster":"" // url to the image again if showing Poster image
         }
      }
   }

So you would need to map over $post->images and return data in the require structure:

$existingFiles = $post->images->map(function ($image) use ($post) {
            return [
                'source' => $image->id,
                'options' => [
                    'type' => 'local',
                    'file' => [
                        'name' => $image->getUrl(),
                        'size' => $image->getSize(),
                        'type' => $image->getMimeType(),
                    ],
                    'metadata' => [
                        'poster' => $image->getUrl(),
                    ],
                ],
            ];

});

I am showing this example with all of the image related properties url, mime etc. encapsulated in the Image model (if that is relevant to your case)

1 like
brentxscholl's avatar

Apologies for the late response (busy week)

Thank you for your clear response!

This works great for displaying the images in Filepond.

How would you go about updating the images on the server side using livewire?

I see you have the $image->id in the source, but how can you access this data in livewire on save?

Thanks again for the help!

brentxscholl's avatar

With the above solution for loading the pre-existing files, how would I handle the images when the user saves? I would need to check if they uploaded new images, and save those to to the file system and db, and I would need to set the order of all the images (which are a mix of pre-existing and new) and reflect it in the database.

With the reordering the real catch is this part here

// This allows me to reorder the images, essential resetting the $image variable in livewire component when ever the user reorders the images in filepond
                onreorderfiles(files, origin, target){
                    @this.set('images', null);
                    files.forEach(function(file) {
                        @this.upload('images', file.file);
                    });
                },

I'm not sure how I could handle reordering images that are both pre-existing and newly uploaded to filepond.

@tykus If you have a fully functioning filepond for when a user edits a model with images, I would really appreciate some advice.

Please or to participate in this conversation.