This driver does not support creating temporary URLs - RuntimeException in file upload (Temporary Preview Urls) livewire temporaryUrl() |
To solve this RuntimeException use PHP try { ... } catch(RuntimeException $e) { ... } handling.
Here's an example of a file upload with an image preview in livewire with the solution of RuntimeException - This driver does not support creating temporary URLs:
<?php
namespace App\Http\Livewire\User\Prescription;
use Livewire\Component;
use Livewire\WithFileUploads;
class UploadePrescriptionLivewire extends Component
{
use WithFileUploads;
public $photo;
public function render()
{
return view('livewire.uploade-prescription-livewire');
}
// This function is for photo realtime upload validation
public function updatedPhoto()
{
$this->validate([
'photo' => 'image|max:1024',
]);
}
public function save()
{
$this->validate([
'photo' => 'image|max:1024', // 1MB Max
]);
$url = $this->photo->store('prescription-photos', 'public');
dd($url); // Use this $url to store the url of your uploaded photo in database
}
}
================================================================
<div>
<form wire:submit.prevent="save">
@if ($photo)
@php
try {
$photoTempUrl = $photo->temporaryUrl();
$photoFlag = true;
}catch (RuntimeException $e){
$photoFlag = false;
}
@endphp
@if($photoFlag)
<img src="{{ $photoTempUrl }}" width="100" height="100">
@else
Something went wrong while uploading the file.
@endif
@endif
<input type="file" wire:model="photo">
@error('photo') <span class="error text-danger"><b>{{ $message }}</b></span> @enderror
<button type="submit" class="btn">Upload Photo</button>
</form>
</div>
No comments:
Post a Comment