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

OfficialEllaris's avatar

File Upload Via API Endpoint (HTTP Facade & Livewire)

I'm trying to upload the avatar via the API endpoint but every time I select a file Livewire resets the field. File upload works well when I tested with Postman. But the issue is using Livewire to consume the endpoint. Live wire just resets the file upload field when an image is selected. The other fields update easily without any issues. It's just the avatar field.

Livewire Form UpdateAccount() Component

<form wire:submit.prevent="submit" enctype="multipart/form-data">
    <div class="widget-edit mb-30 avatar dashed-grey-border">
        <div class="title">
            <h4>Edit your avatar</h4>
            <i class="icon-keyboard_arrow_up"></i>
        </div>
        <div class="uploadfile flex">
            <img src="{{ $avatar ? $avatar->temporaryUrl() ?? asset($avatar) : asset('images/avatar/avatar-08.png') }}"
                style="width: 125px; height: 125px; object-fit: cover; border-radius: 10%;" alt="">

            <div>
                <h6>Upload a new Avatar</h6>
                <label class="dashed-grey-border">
                    <input type="file" name="avatar" wire:model="avatar" accept=".jpeg,.png,.svg,.webp">
                    <span class="text filename">No files selected</span>
                </label>
                @if(session()->has('avatar_error'))
                    <p class="tf-gradient-text mt-3 mb-2 fw-light">
                        {{ session('avatar_error') }}
                    </p>
                @endif
                <p class="text">JPEG, PNG, SVG or WEBP - Max 2MB</p>
            </div>
        </div>
    </div>

	// Other form fields below : Username, Phone Number, DOB, Address etc.
	...
</form>

Livewire UpdateAccount() Component

API Endpoint Handler - handleAccountUpdate()

Helper Function - handleFileUploads()

/**
 * Handles file uploads, replacing existing files if necessary.
 *
 * @param array $data The existing data array where file paths will be stored.
 * @param mixed $entity The entity to check for existing file paths.
 * 
 * @return array The updated data array with new file paths.
 */
public static function handleFileUploads(array $data, $entity): array
{
    foreach (config('storage') as $file_key => $directory) {
        // Check if a file was provided in the request
        if (request()->hasFile($file_key)) {
            // Get the existing file path from the entity 
            $existing_file = $entity->$file_key ?? null;

            // If a file exists, delete it and upload the new file
            if ($existing_file) {
                Storage::disk('public')->delete(str_replace('/storage/', '', $existing_file));
            }

            // Store the new file and update the data array
            $data[$file_key] = Storage::url(request()->file($file_key)->store($directory, 'public'));
        }
    }

    // Return the updated data array
    return $data;
}
0 likes
1 reply
vincent15000's avatar

If the component resets the field, perhaps you need to prevent it from resetting by adding wire:ignore.

Furthermore I don't understand why you need to use an API endpoint with Livewire. Livewire is not designed to work with endpoints (like you would do with VueJS for example).

Please or to participate in this conversation.