File Uploading Laravel Livewire 3 | EP 3
In this post, I will show you how to image upload with preview using laravel liveiwre 3.
In this example, we will create a PhotoUpload Livewire component. This component will feature a form with a file input field and include image validation. The user can select an image, which will then be uploaded to the storage folder using the `WithFileUploads` trait. Additionally, we will save the uploaded image's details into the database.
Step 1: Create PhotoUpload Component
Now here we will create a Livewire component using their command. So run the following command to create an add more component.
php artisan make:livewire PhotoUpload
Now they created files on both paths:
app/Livewire/PhotoUpload.php
resources/views/livewire/photo-upload.blade.php
Now, both files we will update as below for our contact us form.
app/Livewire/PhotoUpload.php
<?php
namespace App\Livewire;
use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\Image;
class PhotoUpload extends Component
{
use WithFileUploads;
public $photo;
public function render()
{
return view('livewire.photo-upload');
}
public function submit(){
$this->validate([
"photo" => "required|image"
]);
$filepath = $this->photo->store("photos");
$image = Image::create([
"title" => "Test",
"filepath" => $filepath
]);
info($image)
}
}
resources/views/livewire/photo-upload.blade.php
<div>
<form wire:submit="submit">
@if($photo)
Photo Preview:
<img src="{{ $photo->temporaryUrl() }}" width="400px"><br/>
@endif
<label>Image:</label>
<input type="file" name="photo" class="form-control" wire:model="photo">
@error("photo")
<p class="text-danger">{{ $message }}</p>
@enderror
<button class="btn btn-success">Submit</button>
</form>
</div>
Step 2: Use Livewire Component
now, we will user counter component in home page. so you need to update home.blade.php file as the following way:
resources/views/home.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Counter') }}</div>
<div class="card-body">
<livewire:photo-upload />
</div>
</div>
</div>
</div>
</div>
@endsection
Run Laravel:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/
Now, you need to register user and go to dashboard:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Install & Setup Laravel Livewire 3 | EP 1
- Laravel Livewire Select2 Not Working with Wizard Form
- Laravel 11 Livewire Wizard Multi Step Form Tutorial
- Laravel 11 Livewire Pagination Tutorial Example
- Laravel Livewire Click Event Example
- Laravel Livewire Dependant Dropdown Example
- Laravel Livewire Select2 Dropdown Example
- Laravel Livewire Datatables Example Tutorial
- Laravel Livewire Load More OnScroll Example
- Laravel Livewire Image Upload Example
- Laravel Livewire Pagination Example
- Laravel Livewire File Upload Tutorial