Php 如何删除和取消链接json格式的多个图像

Php 如何删除和取消链接json格式的多个图像,php,json,laravel,Php,Json,Laravel,我有多个像这样保存在数据库中的图像 ["7541556437392.JPG","9741556437392.JPG"] 我试图传递json decode和存储在数据库中的image参数,但收到一条错误消息 数组到字符串的转换 我的删除控制器 public function forceDestroy($id) { $post = Post::withTrashed()->findOrFail($id); $post->tags()->detach(); //t

我有多个像这样保存在数据库中的图像

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

我试图传递json decode和存储在数据库中的image参数,但收到一条错误消息

数组到字符串的转换

我的删除控制器

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');
}
在我的删除图像方法中,我尝试在删除与图像和图像缩略图相关的帖子时将它们取消链接

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);
    }
}

我想删除所有的图像时,删除有关的图片,该职位。如何解决我的问题?

因为JSON表示数组,所以需要迭代它的元素。试试这个:

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

谢谢它是为删除后工作。当我更新新图像时,你能帮我删除图像吗。我尝试了你的方法,但无法删除旧图像。我尝试更新和删除,但不起作用@IlhamFirmanAshari你对这个问题的答案看起来应该起作用。