Step 1: Creating the Livewire Component
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.
Step 2: Setting Up the Component Logic
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');
}
}
Key Points:
-
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.
Step 3: Building the Form UI
Open the file-upload.blade.php file and create the file upload form:
@if (session()->has('success'))
{{ session('success') }}
@endif
What’s Happening Here?
-
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 thesave()method. -
Error Messages: Displays validation errors dynamically using
@error
Step 4: File Preview
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).
Step 5: Handling Large Files
For larger file uploads, you may need to:
- Update the
post_max_sizeandupload_max_filesize values in yourphp.inifile. - Configure chunked file uploads in Livewire using the
WithFileUploadstrait.
Step 6: Securing File Uploads
To ensure secure file uploads:
-
Restrict File Types: Use
mimesormimetypesin the validation rules. -
Set File Size Limits: Use the
maxvalidation rule to prevent overly large uploads. -
Sanitize File Names: Livewire automatically handles this when using
store().
Final Output
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.
- Notify users of successful uploads.
-
Key Features Covered Today
With the above setup, your file upload component will:
-
-
-
Using the
WithFileUploadstrait 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.
-
-
Related articles
Debugging and Error Handling in Livewire 3.0
Introduction In the last blog Leveraging Alpine Js With livewire 3.0 advanced interactivity, we explored leveraging Alpine.js with Livewire 3.0 for advanced interactivity. Today, we’ll focus on debugging and error handling in Livewire 3.0. Debugging and managing errors effectively ensures smoother development and a better user experience. Step 1: Using Debug Mode Enable Laravel’s debug […]
Leveraging Alpine.js with Livewire 3.0 for Advanced Interactivity
Introduction In the last blog Real time chat application, we built a real-time chat application using Livewire 3.0. Today, we’ll explore how to leverage Alpine.js with Livewire 3.0 to enhance your application’s interactivity. Alpine.js is a lightweight JavaScript framework that complements Livewire perfectly, enabling you to handle client-side interactions seamlessly. Step 1: Why Use Alpine.js […]
Building a Real-Time Chat Application with Livewire 3.0
Introduction In last blog Adding Notifications and Toast Messages in Livewire 3.0, we explored how to add notifications and toast messages in Livewire 3.0. Today, we’ll dive into building a real-time chat application using Livewire 3.0. Real-time chat apps are integral to modern web applications, enabling seamless communication between users. With Livewire’s reactivity, creating such […]

