ld211z's avatar

Laravel Livewire Upload File Issue

Hello everyone,

When i follow the livewire docs and create a new component for uploading a simple file, I am unable to get passed the validation stage.

When my colleague uses the same code base it work for him.

When i select a new file the error retured is: 'The file failed to upload"

When I submit the file the error becomes: 'The file field is required.'

0 likes
13 replies
migsAV's avatar

Add your code so we can help you better.

ld211z's avatar

Thank you @migsav, but the forum wont let me add code the first day i sign up as it thinks that they are links

ld211z's avatar

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithFileUploads;

class TestFileUpload extends Component
{
    use WithFileUploads;

    public $file;

    public function render()
    {
        return view('test-file-upload');
    }

    public function upload()
    {

        $this->validate([
            'file' => 'required|file',
        ]);
        dd('hello');
        dd($this->file);

    }
}

ld211z's avatar

<div>
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="file" wire:model="file" id="">
        @error('file') <span class="error">{{ $message }}</span> @enderror
        <button wire:click="upload">Upload</button>
    </form>
</div>

removed the prevent from the wire:click as it thought the dot was a link

migsAV's avatar

The problem is your form.

<div>
    <form wire:submit.prevent="upload" method="post">
        <input type="file" name="file" wire:model="file" id="">
        @error('file') <span class="error">{{ $message }}</span> @enderror
        <button type="submit">Upload</button>
    </form>
</div>

You also don't need to pass the enctype="multipart/form-data" from what I have seen from others.

durgesh7's avatar

@migsAV i have many input file field ..i want to save it from wizard form livewire..but some of are uploaded and other are not why ? Other make fakepath instead

migsAV's avatar

@durgesh7 I suggest you create a new thread so you can provide your code and I can help you better.

Make sure that each input field has its own variable

<input type="file" name="file_one" wire:model="file_one" id="">
<input type="file" name="file_two" wire:model="file_two" id="">

In your Livewire component

class LayersTable extends Datatable
{
	public $file_one;
    public $file_two;

	// rest of your code
}
ld211z's avatar

@migsav

Changing the form to the code you provided gives me the same errors.

When i select a new file the error retured is: 'The file failed to upload"

When I submit the file the error becomes: 'The file field is required.'

I am wondering if there might be something under the hood that is causing this?

migsAV's avatar

It could be a permissions issue.

Livewire has a folder where it temporarily stores the file you are trying to upload under storage->app->livewire-tmp check if you have the correct permissions for that folder

ld211z's avatar

@migsav

I dont appear to have a folder by that name in this location

ld211z's avatar

The issue was that in my .env file i had

FILESYSTEM_DRIVER=

Once this was removed it started working as normal

3 likes
migsAV's avatar

Happy to hear you got it to work.

In future consider putting FILESYSTEM_DRIVER=local

1 like
migsAV's avatar

Mark your answer as 'best answer' so people will know your question has been resolved

1 like

Please or to participate in this conversation.