We’ll start by creating a new component for handling file uploads:
php artisan make:livewire FileUpload
This command will generate the following files:
app/Livewire/FileUpload.php – The PHP class for the component logic.
resources/views/livewire/file-upload.blade.php – The Blade file for the file upload UI.
This command will generate the following files:
app/Livewire/RegisterUser.php
– The PHP class for the component logic.
resources/views/livewire/register-user.blade.php
– The Blade file for the form UI.
Open the FileUpload.php file and define the property for managing file uploads:
'required|mimes:jpg,jpeg,png,pdf|max:1024', // Max size: 1MB
];
public function save()
{
$this->validate();
// Store the file
$filePath = $this->file->store('uploads', 'public');
session()->flash('success', 'File uploaded successfully: ' . $filePath);
// Reset the file input
$this->reset('file');
}
public function render()
{
return view('livewire.file-upload');
}
}
WithFileUploads
: This trait provides the functionality to handle file uploads in Livewire.
Validation Rules
: Restricts the file type to JPG, JPEG, PNG, and PDF, with a maximum size of 1MB.
store() Method:
: Saves the file to the storage/app/public/uploads directory.
Flash Messages:
: Notifies the user of successful uploads.
Open the file-upload.blade.php file and create the file upload form:
@if (session()->has('success'))
{{ session('success') }}
@endif
wire:model="file"
: Binds the file input to the $file property in the component.
temporaryUrl()
: Generates a temporary URL for previewing the uploaded file before saving it.
wire:submit.prevent="save"
: Prevents the default form submission and triggers the save()
method.
Error Messages: Displays validation errors dynamically using @error
The temporary URL preview is particularly useful for image uploads. It allows users to see the file they are about to upload. This is achieved using:
$file->temporaryUrl()
Note: This feature is only available for local file uploads and will not work if you’re uploading files directly to external storage (e.g., S3).
For larger file uploads, you may need to:
post_max_size
and upload_max_file
size values in your php.ini
file.WithFileUploads
trait.To ensure secure file uploads:
Restrict File Types: Use mimes
or mimetypes
in the validation rules.
Set File Size Limits: Use the max
validation rule to prevent overly large uploads.
Sanitize File Names: Livewire automatically handles this when using store()
.
With the above setup, your file upload component will:
Allow users to upload files interactively.
Display real-time validation error messages.
Preview uploaded files before saving them.
With the above setup, your file upload component will:
Using the WithFileUploads
trait for file uploads.
Handling file validations (type, size) in Livewire 3.0
Displaying file previews with temporaryUrl()
Securely storing uploaded files.
Flashing success messages after file uploads.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.