本文介绍了如何以json格式删除和取消链接多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中保存了多个图像,

I have multiple images saved in database like this.

["7541556437392.JPG","9741556437392.JPG"]

我尝试传递json解码和存储在数据库中的image参数,但是出现错误消息

I try to pass json decode and the image parameter that store in database but I get an error with message

我的删除控制器

public function forceDestroy($id)
{
    $post = Post::withTrashed()->findOrFail($id);
    $post->tags()->detach(); //tag
    $post ->forceDelete();

    $this->removeImage(json_decode($post->image,true));

    Alert::success('Your post has been deleted successfully')->persistent('Close');

    return redirect('admins-blogpost?status=trash');
}

我的删除图片方法,在删除与它们相关的信息时,我尝试将图片和图片缩略图取消链接.

my remove image method, I try to unlink image and image thumbnail together when delete the post related to them.

public function removeImage($image)
{
    if( ! empty($image))
    {
        $imagePath = $this->uploadPath . '/' . $image;
        $ext = substr(strrchr($image, '.'), 1);
        $thumbnail = str_replace(".{$ext}", "_thumb.{$ext}", $image);
        $thumbnailPath = $this->uploadPath . '/' . $thumbnail;
        if(file_exists($imagePath) ) unlink($imagePath);
        if(file_exists($thumbnailPath) ) unlink($thumbnailPath);
    }
}

我要删除所有与图片有关的帖子时删除所有图片.如何解决我的问题?

I want to delete all images when delete the post related to images that. How to fix my problem?

推荐答案

由于JSON表示数组,因此需要迭代其元素.试试这个:

Since your JSON represents an array, you need to iterate over its elements. Try this:

foreach (json_decode($post->image, true) as $image) {
    $this->removeImage($image);
}

这篇关于如何以json格式删除和取消链接多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 05:23
查看更多