使用的是 laravel-admin 的 JSON 组件
问题代码存在与下面的 $form->table 中
protected function form()
{
$form = new Form(new Good());
……
form−>table(′size′,function(form->table('size', function (form−>table(′size′,function(table) {
$table->text(‘sizes’, __(‘规格大小’));
$table->text(‘colors’, __(‘颜色分类’));
$table->text(‘sales’, __(‘价格’));
$table->text(‘sizecostprice’, __(‘成本价格’));
$table->text(‘sizestock’, __(‘库存’));
$table->image(‘sizeimg’, __(‘图片’));
});
……
}
前端展示界面如下:
加上上面代码后,前台界面显示、上传图片操作、确认提交等操作都正常;
添加完成后,再次编辑该数据,也可以正常显示;
问题在于,在编辑页面,不做任何改动直接提交后,$table->image(‘sizeimg’, __(‘图片’)); 的数据丢失了!
尝试查找原因
分别在新增和编辑时,打印 $form 数据,结果显示
新增时:
编辑后提交(不做任何改动,如果编辑时重新上传图片,还是可以正常存储到数据库的):
可以看到,新增时,sizeimg 是存在的,是一个文件对象;编辑提交后,sizeimg 丢失了
发现编辑时 sizeimg 不存在的问题后,考虑到是不是编辑时没有加载该参数 input 元素,所以又到前台编辑页面去查找,结果发现是有的,如下图:
可以看到,sizeimg 跟其他的 $table->text() 的字段都是同样存在的,但是不明白为什么在提交后会丢失
!
最后贴一下关于这个方法官方文档的使用说明:
解决方法
public function setSizeAttribute(KaTeX parse error: Expected '}', got 'EOF' at end of input: …
if (isset(this->attributes[‘size’])){
oldcontent=jsondecode(old_content = json_decode(oldcontent=jsondecode(this->attributes[‘size’],true);
foreach ($extra as KaTeX parse error: Expected 'EOF', got '&' at position 8: key => &̲val) {
// 如果旧数据未删除,且当前不存在图片,则将旧图片路径添加进去
if (isset(oldcontent[old_content[oldcontent[key]) && !isset($val[‘img’])) {
$val[‘img’] = oldcontent[old_content[oldcontent[key][‘img’] ?? ‘’;
}
}
}
this−>attributes[′attr′]=jsonencode(arrayvalues(this->attributes['attr'] = json_encode(array_values(this−>attributes[′attr′]=jsonencode(arrayvalues(extra));
}