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

aaronmeder's avatar

File Upload Component - save() not called?

Hi there

I am trying to create a first File Upload component with Livewire (using Livewire 3 Beta 8). I just followed along the official docs for Getting Started, Components and Uploads. I have no prior experience with Livewire.

The upload works (pretty magically), but the save() function doesn't seem to be called and I can't understand why šŸ¤·šŸ»ā€ā™‚ļø

I've created a minimal demo with the UploadFiles component right on the welcome view: https://phpsandbox.io/e/x/w6bou?layout=EditorPreview&defaultPath=%2F&theme=dark&showExplorer=no&openedFiles=

Here are some code details, but you can browse the whole app on the phpsandbox link above:

Livewire Component - UploadFiles.php

<?php

namespace App\Livewire;

use Livewire\Attributes\Rule;
use Livewire\Component;
use Livewire\WithFileUploads;

class UploadFiles extends Component
{
    use WithFileUploads;

    #[Rule(['files.*' => 'pdf|max:20480'])]
    public $files = [];

    public $iteration = 0;

    public function save()
    {
        // this function doesn't seem to be called on upload?
        dd("save reached");

        // Store files in temp directory
        foreach ($this->files as $file) {
            $file->store('files');
        }

        // Reset Upload Form
        $this->files = null;
        $this->iteration++;
    }

    public function render()
    {
        return view('livewire.upload-files');
    }
}

Rendered view of the component -upload-files.blade.php

<div>
    <form wire:submit="save">
        <div
            x-data="{ uploading: false, progress: 0 }"
            x-on:livewire-upload-start="uploading = true"
            x-on:livewire-upload-finish="uploading = false"
            x-on:livewire-upload-error="uploading = false"
            x-on:livewire-upload-progress="progress = $event.detail.progress"
        >
            @error('files.*') <span class="error">{{ $message }}</span> @enderror

            @if($files)
                Just Uploaded:<br />
                @foreach ( $files as $file )
                    <a href="{{ $file->temporaryUrl() }}" target="_blank">File</a><br />
                @endforeach
            @endif

            <input type="file" wire:model="files" name="files" id="upload{{ $iteration }}" multiple>

            <!-- Progress Bar -->
            <div x-show="uploading">
                <progress max="100" x-bind:value="progress"></progress>
            </div>

            <button type="submit" :disabled="uploading">Upload files</button>
        </div>
    </form>
</div>

The component is embeded in the welcome view like this:

<livewire:upload-files />

Livewire and Alpine scripts should be automatically injected, as I am reading in the Livewire Docs.

Even when I rename the save() function the upload seems to work, so I am guessing the use WithFileUploads makes that work even without explicitly defining it. Changing the <form wire:submit="save"> to wire:submit="somethingelse" and renaming the function in the component also didn't help.

What am I missing?

Thanks a lot for helping out. Aaron

0 likes
4 replies
LaryAI's avatar
Level 58

It seems that the save() function in the UploadFiles component is not being called when the form is submitted. There are a few possible reasons for this issue:

  1. Make sure that the Livewire JavaScript library is included in your project. You can check if it's included by inspecting the page source code and looking for the Livewire script tag. If it's not included, you can add it by running the following command in your terminal:
npm install livewire

Then, include the Livewire script in your HTML layout file:

<script src="{{ asset('vendor/livewire/livewire.js') }}"></script>
  1. Check if there are any JavaScript errors in the browser console. JavaScript errors can prevent Livewire from working properly. Fix any errors that you find.

  2. Ensure that the Livewire directives are properly used in the component's view file (upload-files.blade.php). The wire:submit directive should be placed on the form element, and the wire:model directive should be used on the file input element.

  3. Verify that the Livewire component is properly registered in your application. You can do this by checking the web.php routes file and ensuring that the Livewire component is included in the routes/web.php file:

use App\Http\Livewire\UploadFiles;

Route::livewire('/upload-files', UploadFiles::class);

If the component is not registered, Livewire won't be able to handle the form submission.

  1. Check if there are any Livewire middleware that might be interfering with the form submission. Livewire provides a protected $middleware property in the component class that allows you to specify middleware that should be applied to the component. Make sure that there are no middleware that could prevent the save() function from being called.

If none of these solutions work, it might be helpful to provide more information about your project setup and any error messages that you are encountering.

aaronmeder's avatar

@LaryAI As far as I can tell none of these points are applicable. #3 seems to relate to Full-page components, which this is not.

Also the upload works, and if I rename the render method of the Livewire Function it spits out an error, so it seems to generally load the file.

Snapey's avatar
Snapey
Best Answer
Level 122

Did you change something on the sandbox? I see "save reached" ?

1 like
aaronmeder's avatar

@Snapey Oh goodness - you are so right...and now I realize my mistake: I never actually clicked on the "Upload Files" Button because it had already uploaded the files to /livewire-tmp/ and I didn't realize it should do more on submit šŸ™ˆ

Of course: as soon as I click on submit / "Upload files" it will call the function as expected.

Silly mistake by me ;)

Please or to participate in this conversation.