spatie/livewire-filepond
Laravel Livewire component that integrates FilePond for modern, smooth file uploads. Drop in <x-filepond::upload wire:model="file" />, add the provided scripts, and handle temporary uploads seamlessly in your Livewire forms with minimal setup.
Installation:
composer require spatie/livewire-filepond
Publish the config (optional):
php artisan vendor:publish --provider="Spatie\LivewireFilepond\LivewireFilepondServiceProvider"
First Use Case:
<x-filepond::upload wire:model="file" />
public $file;
public function mount()
{
$this->file = null;
}
public function rules()
{
return [
'file' => 'nullable|file|max:10240', // 10MB
];
}
Where to Look First:
wire:model integration.Basic File Upload:
<x-filepond::upload wire:model="file" /> for single-file uploads.public function updatedFile()
{
$this->validate(['file' => 'required|file|mimes:pdf,jpg,png']);
if ($this->file) {
$path = $this->file->store('uploads');
// Process $path (e.g., save to DB)
}
}
Multiple Files:
wire:model="files" (array) and loop through the results:
<x-filepond::upload wire:model="files" multiple />
public $files = [];
public function updatedFiles()
{
$this->validate([
'files.*' => 'file|max:10240',
]);
if (!empty($this->files)) {
foreach ($this->files as $file) {
$path = $file->store('uploads');
// Process each file
}
}
}
Custom Filepond Options:
options prop:
<x-filepond::upload
wire:model="file"
:options="['allowFileType' => ['image/jpeg', 'image/png']]"
/>
config/livewire-filepond.php.Integration with Storage:
Storage facade to handle files:
use Illuminate\Support\Facades\Storage;
public function updatedFile()
{
if ($this->file) {
Storage::disk('s3')->putFile('uploads', $this->file);
}
}
Progress Tracking:
wire:ignore to show a progress bar:
<div wire:ignore>
<progress wire:model="uploadProgress"></progress>
</div>
public $uploadProgress = 0;
public function updatedFile()
{
$this->uploadProgress = 100; // Example
}
Reusable Components:
php artisan make:livewire FileUpload
<!-- resources/views/livewire/file-upload.blade.php -->
<x-filepond::upload wire:model="file" />
// App\Http\Livewire\FileUpload
public $file;
public function mount($model = null) { ... }
File Validation Timing:
updatedFile fires before validation, so validate in rules() or updatedFile:
public function rules()
{
return ['file' => 'nullable|file|max:10240'];
}
updatedFile if needed.Filepond JS Conflicts:
vendor.js).filepond.css).?v=2 to bust cache.Large File Handling:
<x-filepond::upload wire:model="file" chunkUploads />
config/livewire-filepond.php:
'chunk_size' => '5MB',
Livewire State Persistence:
$this->persist() or session storage if needed:
protected $persistFiles = true; // Add to Livewire component
Server-Side Processing:
/livewire/update, not a custom endpoint. For custom logic, use a form submission:
<form wire:submit.prevent="customUpload">
<x-filepond::upload wire:model="file" />
<button type="submit">Upload</button>
</form>
public function customUpload()
{
$this->validate(['file' => 'required']);
// Custom logic here
}
Check Livewire Logs:
config/livewire.php:
'log' => env('APP_DEBUG'),
storage/logs/livewire.php.Filepond Console Errors:
F12) and check the Console tab for Filepond errors (e.g., missing plugins).Network Requests:
/livewire/update.Common Errors:
filepond.js or filepond.css./livewire/update) is accessible.@csrf to your Blade template if using standalone forms.Custom Plugins:
FilePondPluginImagePreview):
<x-filepond::upload
wire:model="file"
:options="[
'plugins' => [
'FilePondPluginImagePreview',
'FilePondPluginFileValidateType',
],
]"
/>
resources/js/app.js:
import 'filepond/dist/filepond.min.css';
import FilePond from 'filepond';
import 'filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js';
Dynamic Configuration:
<x-filepond::upload
wire:model="file"
:options="['maxFiles' => $this->maxFiles]"
/>
Event Listeners:
init, addfile) via JavaScript:
<script>
document.addEventListener('livewire:init', () => {
Livewire.on('filepond-init', () => {
console.log('Filepond initialized!');
});
});
</script>
$this->dispatch('filepond-init');
Custom Storage Engines:
use Spatie\LivewireFilepond\LivewireFilepond;
class CustomFilepond extends LivewireFilepond
{
public function storeFile($file)
{
return Storage::disk('s3')->put('custom-path', file_get_contents($file));
}
}
AppServiceProvider:
Livewire::component('filepond.upload', CustomFilepond::class);
Localization:
config/livewire-filepond.php:
'labels' => [
'fileTypeNotAllowed' => 'This file type is not allowed.',
],
How can I help you explore Laravel packages today?