kazzuya's avatar

Image Update

Hello, When im editing an image it's uploading a new image without remove the old one, I've tried a lot but nothing happend

	    public function showEditModal($id)
{
    $this->categories = Category::findOrFail($id);
    $this->category_name = $this->categories->category_name;
    $this->oldImage = $this->categories->category_image;
    $this->isEditMode = true;
    $this->showingPostModal = true;
}

public function updatePost()
{
    $this->validate([
        'category_name' => 'required',
    ]);

    $image = $this->categories->category_image;

    if($this->category_image){
        $image = $this->category_image->store('public/category_image');
    }

    $this->categories->update([
        'category_name' => $this->category_name,
        'category_image' => $image
    ]);

    $this->reset();
}

Here is my code, Any Help please ?

0 likes
5 replies
vincent15000's avatar

That's normal, uploading doesn't replace automatically the previous image. You have to delete the previous one manually.

kazzuya's avatar

@vincent15000 i was using this without livewire

        if ($request->hasfile('file')) {
        $destination = "cover/" . $category->file;
        if (File::exists($destination)) {
            File::delete($destination);
        }
        $image = $request->file('file');
        $extension = $image->getClientOriginalExtension();
        $filename = time() . '.' . $extension;
        $image->move('cover/', $filename);
        $category->file = $filename;
    }

Nothing similiar in livewire ?

1 like
vincent15000's avatar

@kazzuya Livewire is nothing else than PHP. So why not using (and adapting) the same code to remove the previous image ?

newbie360's avatar

@kazzuya if you know what the code means, you wouldn't ask this question, just get the filename from database and delete the file via any file operation

$filename = time() . '.' . $extension;
1 like
kazzuya's avatar

@newbie360 to be honest your words was op, But u make challenge myself to do it, Now i can only say thank you for that... It Works fine

1 like

Please or to participate in this conversation.