I try to upload a file in livewire but I faced some problems
this is my code
my component code is
<?php
namespace AppLivewire;
use AppModelsbrand;
use Exception;
use LivewireComponent;
use LivewireWithFileUploads;
use LivewireWithPagination;
class LiveBrand extends Component
{
use WithPagination;
use WithFileUploads;
public $search;
public $t_name;
public $sort = '';
public string $name = '';
public $logo;
public $namb = 5;
public bool $stat = false;
public function stat()
{
$this->stat = !$this->stat;
}
public function show()
{
$this->namb++;
}
public function stor()
{
try {
$validated = $this->validate([
'name' => 'required|string|max:255|min:2',
'logo' => 'required|image|max:1024', // 1MB Max
'stat' => 'nullable|boolean'
]);
// Check if logo exists
if ($this->logo) {
$imagePath = $this->logo->store(path: 'public');
Brand::create([
'name' => $validated['name'],
'logo' => $imagePath,
'stat' => $validated['stat'] ?? false
]);
$this->reset(['name', 'logo', 'stat']);
$this->create_alert('Brand');
$this->dispatch('brand-created');
} else {
session()->flash('error', 'Please select a logo file');
}
} catch (Exception $e) {
session()->flash('upload_error', 'File upload failed: ' . $e->getMessage());
$this->exception_alert($e);
}
}
public function sorter($name)
{
$this->sort = $this->sort === 'desc' ? 'asc' : 'desc';
$this->t_name = $name;
}
public function render()
{
$all = brand::where('name', 'like', "%{$this->search}%")
->orWhere('created_at', 'like', "%{$this->search}%")
->orderBy(empty($this->t_name) ? 'id' : '' . $this->t_name . '', '' . empty($this->sort) ? 'desc' : '' . $this->sort . '' . '')
->latest()
->paginate(5);
$tabelnames = [
"Brand" => 'name',
"Logo" => 'logo',
"Créé Le" => 'created_at',
"Statut" => 'stat'
];
return view('livewire.live-brand', [
"thname" => $tabelnames,
"all" => $all,
"uploadError" => session('upload_error')
]);
}
}
me view is
<div class="page-wrapper row">
<form wire:submit="stor">
@error('logo')
<div classs="alert alert-danger text-danger">{{$message}}</div>
@enderror
<div class="col-12">
<label for="" class="form-label">Name</label>
<input class="form-control" type="text" wire:model="name">
</div>
<div class="col-12">
<label for="" class="form-label">Logo</label>
<input class="form-control" type="file" wire:model="logo">
</div>
<button class="btn btn-primary" type="submit">Upload</button>
</form>
</div>
it show me this error The logo failed to upload. and when I click on the upload button it show me the The logo field is required please help me .
enter image description here