ahmeda's avatar

Upload file in livewire ?

I have a page to update the image profile of the user, and there is in three things:

  • if the user does not have any image just show the default one
  • If the user has one just show it
  • if the user has one and wants to change to a new one, preview it!

Component:

class UpdateMainSetting extends Component
{
    use WithFileUploads;

    public $user;
    public $avatar;
    public $name;

    public function mount($user)
    {
        $this->user = $user;
        $this->avatar =  $user->avatar;
        $this->name = $user->name;
    }

    public function update()
    {
        $image = [
            'name' => $this->avatar->hashName(),
            'path' => $this->avatar->getRealPath(),
        ];

        if($this->avatar){
            Storage::disk('s3')->put('uploads/merchant/avatar/'.$image['name'], file_get_contents($image['path']), 'public');
        }

        $this->user->update([
            'email' => $this->email,
            'name' => $this->name,
            'avatar' => $this->avatar,
        ]);
    }

blade component:

@if ($avatar)
    <img src="{{ $avatar->temporaryUrl() }}">
@elseif (auth()->user()->avatar)
    <img src="{{ auth()->user()->avatar }}">
@else
    <img src="{{ auth()->user()->defualt }}">
@endif

All thing is worked with the store and show the image but I got an issue with the user upload an image then want to change it to a new one the temporaryUrl () does not work (it returns the value of the image that I store it in the database )

How can a preview image if the user already had an image?

0 likes
0 replies

Please or to participate in this conversation.