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

brentxscholl's avatar

Handling File uploads and reorder with Laravel Livewire and Filepond

I have a form in my application that allows users to create posts and while doing so upload multiple images to the post being created.

I am using Laravel Livewire and Filepond to achieve this.

The problem I am having is I need to allow the user to reorder the images (as it is a gallery and the order is important), and save the order in the database when the form in submitted.

Another issue I am running into is allowing a user to edit their post later. I need their pre-existing post images loaded in filepond, and also allow them to upload more, delete, and/or reorder.

When the user saves the post I need to be able to update my database and file system.

I've been trying to figure this out for days with no luck. All info online is how to upload files, but no info on how to reorder, or prepopulate with pre-existing files.

Any help with this would be greatly appreciated.

Here is my current code for reference:

<div
    x-data=""
    x-init="
       
        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)
                },
                load: (source, load, error, progress, abort, headers) => {
                    var myRequest = new Request(source);
                    fetch(myRequest).then(function(response) {
                      response.blob().then(function(myBlob) {
                        load(myBlob)
                      });
                    });
                },
            },
        });
        const pond = FilePond.create($refs.input, {
            acceptedFileTypes: ['image/png', 'image/jpeg'],
            maxFileSize: '7MB',
            allowImageCrop: true,
            allowReorder: true,
            allowImageResize: true,
            imageResizeTargetWidth: '1000px',
            imageResizeTargetHeight: '1000px',
            filePosterMaxHeight: '256px',
            files: {{ $existingImages }} // used for when editing a post and it already has images. see php component on how I set this variable
            
        });

    "
>
    <div wire:ignore wire:key="images">
        <div class="form-group text-center">
            <input
                id="image-upload"
                type="file"
                x-ref="input"
                multiple
                data-allow-reorder="true"
                data-max-file-size="3MB"
                data-max-files="10"
            >
        </div>
    </div>
</div>

My livewire PHP component:

    public $images = [];
    public $existingImages;

    public function mountMedia($post) {
        if($post){
            $this->existingImages = $post->images->map(function ($image) use ($post) {
                return [
                    'source' => $image->id,
                    'options' => [
                        'type' => 'local',
                        'file' => [
                            'name' => $image->getUrl(),
                            'size' => $image->file_size,
                            'type' => $image->mime_type,
                        ],
                        'metadata' => [
                            'poster' => $image->getUrl(),
                            'position' => $image->position
                        ],
                    ],
                ];

            });
        }
    }
    public function saveImage($file, $post, $position) {
        // Create a unique random string
        $randString = Str::random(3);
        // Get time
        $time = time();
        // Set file name
        $filename = $time. '-' . $randString.'-'.auth()->user()->id;
        $extension = '.'.$file->getClientOriginalExtension();

        // Save images for gallery 
        $regImage = $file->storeAs('/'. $post->id, $filename.$extension, 'post_images');

           
        // Create a new image in db
        Image::create([
            'user_id'       => auth()->user()->id,
            'post_id'       => $post->id,
            'position'      => $position,
            'filename'      => $filename,
            'extension'     => $extension,
            'src'           => 'post_images',
            'mime_type'     => $file->getMimeType(),
            'file_size'     => $file->getSize(),
        ]);
    }

    public function saveMedia($post) {
        // Make sure user owns post
        abort_unless($post->user_id == auth()->user()->id, 403);

        // Set default position
        $position = 1;

        // Save each image
        foreach ($this->images as $file) {
           $this->saveImage($file, $post, $position);
            // Increment position for next image
            $position++;
        }
    }
}
0 likes
4 replies
sr57's avatar

Hi @brentxscholl

Think you have to code the 'bricks' and assemble them as you want, some ideas to do it :

-1- make icons (small size) of all your already downloaded images

-2- keep an uid in your db referencing your image and icon

-3- decide how you want to do the reorder

-3.1- show all your icons and move them with the mouse

-3.2- affect an order number to each icon and change it via text or select

-3.2..1- you are not force to have unique order number, if not unique some images may be not well order, but user can always change this by entering new number order

-4-create code/button to delete or add new image

In a nutshell, always cut your task in small ones and you'll probably find examples for each one, if not ask for help here but for the overall task your are the only one who knows what you want to do.

brentxscholl's avatar

Thanks @sr57 I'm aware of the steps I need to take. This does not help my issue.

Filepond has the ability to reorder and preload files. I'm looking for help with the API to make this work when saving post with images in livewire. How do I get the position of the images in filepond when saving to the database? Livewire uses temp files when uploaded, it is unaware of the changes in file pond, unless I reupload them again, but this causes issues with the UX of the form. That being said I'm trying to understand how I would combine the temp files livewire creates on upload with previously uploaded files in the same Filepond instance and be able to reorder and save.

Tweekah's avatar

You ever got the solution? Very curious.

Please or to participate in this conversation.