i have been checking the docs still not getting why its not working when i clicked on save nothing is happenning when i check database no image is been uploaded , what i am trying to do is for admin to able to update or upload avatar for users
<?php
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
class AdminUser extends Component
{
use WithPagination;
use WithFileUploads;
public $showModal = false;
public $Userid;
public $user;
public $creator_name;
public $username;
public $name;
public $avatar;
public $verify = false;
protected $rules = [
'user.name' => 'required',
'user.email' => 'required|email',
'user.username' => 'required',
'user.phone_number' => '',
'user.creator_name' => '',
'user.creator_bio' => '',
// must add addition fields here
];
public function mount()
{
$this->user = new User;
}
public function edit(User $user)
{
$this->showModal = true;
$this->user = $user;
$this->updateMode = true;
}
public function cancel()
{
$this->showModal = false;
$this->updateMode = false;
$this->reset();
}
public function close()
{
$this->showModal = false;
}
public function save()
{
// dd($this->user);
$this->validate();
// $this->avatar->storeAs('avatar', 'public');
$this->user->save();
$this->updateMode = false;
session()->flash('message', 'Users Updated Successfully.'); // this works?
$this->reset();
}
public function updateAvatar()
{
$this->validate([
'avatar' => 'image|max:1024', // 1MB Max
]);
$filename = $this->avatar->storeAs('public', 'avatar');
auth()->user()->update([
'avatar' => $filename,
]);
}
public function render()
{
$users = User::orderBy('id','desc')->simplePaginate(10);
return view('livewire.admin-user',[
'users' => $users,
]);
}
}
livewire blade
<form wire:submit.prevent="updateAvatar">
@if ($user->avatar)
Photo Preview:
<img src="{{ $user->avatar->temporaryUrl() }}">
@endif
<input type="file" wire:model="avatar">
@error('avatar') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Save Photo</button>
</form>