Laravel-admin: 请问怎么更改图片后而不删除源图片

Created on 5 May 2019  ·  2Comments  ·  Source: z-song/laravel-admin

  • Laravel Version: #.#.#
  • PHP Version:
  • Laravel-admin: #.#.#

Description:

不是做默认,而是在做商品快照是会存在图片引用,这个怎么解决?

    public function update($id)
    {
        $commodity = Commodity::query()->findOrFail($id);
        try {
            $file = Storage::disk()->get($commodity->thumbnail);
        } catch (FileNotFoundException $e) {
        }

        $updated = $this->form()->update($id);

        $file && Storage::disk()->put($commodity->thumbnail, $file);
        return $updated;
    }

重写Image?

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/5
 * Time: 16:57
 */

namespace App\Admin\Extensions;


use Encore\Admin\Form\Field\Image;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class HoldImage extends Image
{
    protected function uploadAndDeleteOriginal(UploadedFile $file)
    {
        $this->renameIfExists($file);

        $path = null;

        if (!is_null($this->storage_permission)) {
            $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name, $this->storage_permission);
        } else {
            $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name);
        }

        return $path;
    }
}

Steps To Reproduce:

Most helpful comment

就按照你上面写的重写一个image就好了

class HoldImage extends Image
{
    protected $hold = false;

    public function hold()
    {
        $this->hold = true;

        return $this;
    }

    public function destroy()
    {

        if ($this->hold) {
            return;
        }

        if ($this->storage->exists($this->original)) {
            $this->storage->delete($this->original);
        }
    }
}

All 2 comments

就按照你上面写的重写一个image就好了

class HoldImage extends Image
{
    protected $hold = false;

    public function hold()
    {
        $this->hold = true;

        return $this;
    }

    public function destroy()
    {

        if ($this->hold) {
            return;
        }

        if ($this->storage->exists($this->original)) {
            $this->storage->delete($this->original);
        }
    }
}

很有用的功能,能不能把这个hold方法加到Image中,这样不需要新建组件也能控制删除开关

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chenyongmin picture chenyongmin  ·  3Comments

MarKco picture MarKco  ·  3Comments

evans-kim picture evans-kim  ·  3Comments

zhenyangze picture zhenyangze  ·  3Comments

amun1303 picture amun1303  ·  3Comments