【问题标题】:how to update and remove (unlink) multiple old images in json format如何更新和删除(取消链接)多个 json 格式的旧图像
【发布时间】:2019-09-17 05:50:15
【问题描述】:

这仍然与我在这里How to remove and unlink multiple images in json format的帖子有关

我可以存储和删除所有多个图像。但是更新和删除旧图像时我仍然有问题。更新新图像时,我的代码仍然没有删除旧图像。

我的更新控制器,我使用 handlerequest 方法来存储图像,其中 oldImage 是数据库中的变量,它将与新图像进行比较。如果不一样,则删除旧图像。

public function update(Requests\UpdatePostRequest $request, $id)
{

    $post = Post::findOrFail($id);
    $oldImage = $post->image;
    $data = $this->handleRequest($request);
    $post->update($data);


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

    if($request->submitbutton =='Publish')
    {
        Alert::success('Your post was updated successfully')->persistent('Close');
        return redirect(route('admins-blogpost.index'));
    }
    if($request->submitbutton =='Save Draft')
    {
        Alert::success('Your draft was updated successfully')->persistent('Close');
        return redirect(route('admins-blogpost.index'));
    }
}

我的handlerequest 方法来存储多个图像,这个方法是使用store 方法工作的。所以这个问题不在这里。

private function handleRequest($request)
{
    $data = $request->all();

    if($request->hasFile('image'))
    {
        $images = $request->file('image');
        foreach($images as $key=>$image){
            $fileName    = $image->getClientOriginalName();

            $destination = $this->uploadPath;

            $successUploaded = $image->move($destination, $fileName);
            if($successUploaded)
            {
                $width = config('cms_blog.image.thumbnail.width');
                $height = config('cms_blog.image.thumbnail.height');
                $extension = $image->getClientOriginalExtension();
                $thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
                Image::make($destination . '/' . $fileName)
                        ->resize($width,$height)
                        ->save($destination . '/' . $thumbnail);
                $datax[] = $fileName;
            }
            $data['image'] = json_encode($datax);
        }

    }
    return $data;
}

删除方法

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

如何解决我的问题,以便在旧图像与新图像不同时删除旧图像?

【问题讨论】:

    标签: php json laravel


    【解决方案1】:

    在删除图像的 for 循环中,我相信您想迭代 $oldImage,而不是 $post->image

    foreach (json_decode($oldImage, true) as $item) {
        $this->removeImage($item);
    }
    

    【讨论】:

    • 谢谢。我不专注。是的,正因为如此,我无法删除旧图像
    【解决方案2】:

    在单个列上存储多个图像名称,这不是一个好主意。我建议你创建另一个表 post_images 或其他东西。

    并从posts 表和post_images 表创建hasMany 关系。那么你在这里有很大的灵活性。此外,您可以创建多态关系以将同一图像模型与多个模型一起使用。

    【讨论】:

    • 好的。谢谢你的建议。您可以使用链接或我可以学习的东西分享信息。在我的大脑中,我需要存储与我的单个帖子相关的图像的图像 ID?如果我需要从一个 ID 显示更多图像,那么我需要更多图像 ID 怎么办?在另一个表上,例如 image table,如果一个 id 存储多个名称,那么它是相同的。我只是添加数据库表。
    猜你喜欢
    • 2019-10-30
    • 2020-10-24
    • 1970-01-01
    • 2011-02-22
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多