不是做默认,而是在做商品快照是会存在图片引用,这个怎么解决?
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;
}
}
就按照你上面写的重写一个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中,这样不需要新建组件也能控制删除开关
Most helpful comment
就按照你上面写的重写一个image就好了