Home > Software engineering >  How to delete a post together with image uploaded with summernote
How to delete a post together with image uploaded with summernote

Time:07-31

The following is my store and destroy method respectively. Everything works as expected but when I delete a post it gets deleted but the image that is uploaded with the summer note doesn't. How can I go about achieving that?

public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required | max:100',
            'category' => 'required',
            'tags' => 'required',
            'short_description' => 'required | max:200',
            'image' => 'required | image',
            'content' => 'required'
        ]);

        $title = $request->title;
        $category_id = $request->category;
        $short_description = $request->short_description;
        $mainImage = $request->file('image');
        $content = $request->content;
        $tags_id = $request->tags;
        $user_id = Auth::user()->id;

        $dom = new \DomDocument();
        $dom->loadHtml($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $imageFile = $dom->getElementsByTagName('img');

        foreach ($imageFile as $item => $image) {
            $data = $image->getAttribute('src');
            list($type, $data) = explode(';', $data);
            list(, $data)      = explode(',', $data);
            $imgeData = base64_decode($data);
            $image_name = "/summernoteUploads/" . time() . $item . '.png';
            $path = public_path() . $image_name;
            file_put_contents($path, $imgeData);

            $image->removeAttribute('src');
            $image->setAttribute('src', $image_name);
        }

        $content = $dom->saveHTML();
        $post = Post::create([
            'title' => $title,
            'category_id' => $category_id,
            'short_description' => $short_description,
            'content' => $content,
            'user_id' => $user_id,
        ]);
        $post->tags()->attach($tags_id);
        $post->addMedia($mainImage)->toMediaCollection();

        return redirect(route('posts.dashboard.index'))->with('status', 'Post created Successfully');
    }



 public function destroy(Post $post)
    {
        $post->delete();
        return redirect()->back()->with('status', 'Post Deleted Successfully');
    }

CodePudding user response:

For doing this, you can use an observer or simply use model events:

protected static function booted()
{
    static::deleted(function ($model) {
        // Soft deleted, do what you want.
    });

    static::forceDeleted(function ($model) {
        // Force deleted, do what you want.
    });
}

CodePudding user response:

if you upload images in server and save the path in database do this function:

public static function delete_image($image_path)
{
    $image_path = public_path('uploads\\' . Str::replace('/', '\\', $image_path));
    if (file_exists($image_path))
    {
        @unlink($image_path);
        return true;
    }
}

just send the path of image.

  • Related