Add your code so we can help you better.
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.'
Thank you @migsav, but the forum wont let me add code the first day i sign up as it thinks that they are links
<?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);
}
}
<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
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.
@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
@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
}
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?
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
I dont appear to have a folder by that name in this location
The issue was that in my .env file i had
FILESYSTEM_DRIVER=
Once this was removed it started working as normal
Happy to hear you got it to work.
In future consider putting FILESYSTEM_DRIVER=local
Mark your answer as 'best answer' so people will know your question has been resolved
Please or to participate in this conversation.