File uploads are a common requirement in web applications. While basic file uploads are straightforward to implement, advanced scenarios often require handling multiple files, validating uploads, providing user feedback, and integrating with cloud storage. Livewire, a full-stack framework for Laravel, simplifies these tasks by providing robust tools for handling file uploads. This article will explore how to implement advanced file upload scenarios with Livewire.
Setting Up Livewire
Before diving into advanced file uploads, ensure you have Livewire installed and set up in your Laravel project. If you haven't already, you can install Livewire by following these steps:
1. Install Livewire via Composer:
composer require livewire/livewire
2. Include Livewire Scripts and Styles:
Add the Livewire styles and scripts to your Blade template:
<!-- resources/views/layouts/app.blade.php -->
<!DOCTYPE html>
<html>
<head>
@livewireStyles
</head>
<body>
@yield('content')
@livewireScripts
</body>
</html>
Basic File Upload with Livewire
Let's start with a basic file upload example to lay the foundation:
1. Create a Livewire Component:
php artisan make:livewire FileUpload
2. Define the Component Logic:
// app/Http/Livewire/FileUpload.php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithFileUploads;
class FileUpload extends Component
{
use WithFileUploads;
public $file;
public function upload()
{
$this->validate([
'file' => 'required|file|max:10240', // 10MB Max
]);
$this->file->store('uploads');
session()->flash('message', 'File uploaded successfully.');
}
public function render()
{
return view('livewire.file-upload');
}
}
3. Create the Blade View:
<!-- resources/views/livewire/file-upload.blade.php -->
<div>
@if (session()->has('message'))
<div>{{ session('message') }}</div>
@endif
<form wire:submit.prevent="upload">
<input type="file" wire:model="file">
@error('file') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Upload</button>
</form>
</div>
Advanced File Upload Scenarios
Now that we have a basic file upload example, let's explore some advanced scenarios.
1. Multiple File Uploads
To handle multiple file uploads, modify the component to accept an array of files:
// app/Http/Livewire/FileUpload.php
public $files = [];
public function upload()
{
$this->validate([
'files.*' => 'required|file|max:10240',
]);
foreach ($this->files as $file) {
$file->store('uploads');
}
session()->flash('message', 'Files uploaded successfully.');
}
Update the Blade view to support multiple files:
<!-- resources/views/livewire/file-upload.blade.php -->
<form wire:submit.prevent="upload">
<input type="file" wire:model="files" multiple>
@error('files.*') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Upload</button>
</form>
2. Progress Bar for File Uploads
Livewire makes it easy to provide real-time feedback on upload progress. Use the `wire:loading` directive to show a progress bar:
<!-- resources/views/livewire/file-upload.blade.php -->
<form wire:submit.prevent="upload">
<input type="file" wire:model="files" multiple>
@error('files.*') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Upload</button>
<div wire:loading wire:target="files">Uploading...</div>
</form>
3. Chunked File Uploads
For large files, chunked uploads can be more efficient. Livewire supports chunked uploads out of the box:
// app/Http/Livewire/FileUpload.php
public function upload()
{
$this->validate([
'file' => 'required|file|max:102400', // 100MB Max
]);
$this->file->storeAs('uploads', 'large-file.zip', ['disk' => 'local', 'chunkSize' => 1024 * 1024]);
}
4. Uploading to Cloud Storage
To upload files directly to cloud storage (e.g., AWS S3), configure the storage in `config/filesystems.php` and modify the `store` method:
// app/Http/Livewire/FileUpload.php
public function upload()
{
$this->validate([
'file' => 'required|file|max:10240',
]);
$this->file->store('uploads', 's3');
}
Livewire provides a powerful and flexible way to handle file uploads in Laravel applications. By leveraging Livewire's features, you can implement advanced file upload scenarios such as multiple file uploads, progress bars, chunked uploads, and uploading to cloud storage with ease. This approach enhances user experience and simplifies the handling of complex file upload requirements.
0 Comments