Kikismedia's avatar

livewire unable to upload avatar

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>
0 likes
6 replies
webrobert's avatar
Level 51

@kikismedia,

public function updateAvatar()
    {
        $this->validate([
            'avatar' => 'image|max:1024', // 1MB Max
        ]);

       $filename = $this->avatar->storeAs('public', 'avatar'); // this is going to name all the photos 'avatar'. 

        auth()->user()->update([ 
            'avatar' => $filename,
        ]);
    }

https://laravel-livewire.com/docs/2.x/file-uploads#storing-files

EDIT

//  update the user in edit mode...
$this->user->update([
    'avatar' => $this->avatar->store('avatars'),
]);
<form wire:submit.prevent="updateAvatar">
    @if ($user->avatar)  <!-- this should be $avatar -->
        Photo Preview:
        <img src="{{ $user->avatar->temporaryUrl() }}"> <!-- this should be $avatar->temporaryUrl() -->
    @endif
    <input type="file" wire:model="avatar">

    @error('avatar') <span class="error">{{ $message }}</span> @enderror

    <button type="submit">Save Photo</button>
</form>

https://laravel-livewire.com/docs/2.x/file-uploads#preview-urls

Kikismedia's avatar

@webrobert the preview is working , but i noticed its only my avatar that is been updated? if i try to edit other user its not working for them

webrobert's avatar

@Kikismedia, I revised my answer. But I don't understand how this works. You have a list of users. Assuming you're an admin? But the profile update is working off auth()->user? So the admin is going to update anyone's image to their own? 🤷🏼‍♂️ I recognize my notes in the component from before. :)

Kikismedia's avatar

@webrobert yes i have a list of users and i want the admin should be able to edit each user profile avatar

webrobert's avatar

@Kikismedia

Then assuming the user is still active from edit …

$this->user->update([
    'avatar' => $this->avatar->store('avatars'),
]);

Please or to participate in this conversation.