使用的是 laravel-admin 的 JSON 组件
 问题代码存在与下面的 $form->table 中
protected function form()
 {
$form = new Form(new Good());
 ……  
f
o
r
m
?
>
t
a
b
l
e
(
′
s
i
z
e
′
,
f
u
n
c
t
i
o
n
(
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’])){  
o
l
d
c
o
n
t
e
n
t
=
j
s
o
n
d
e
c
o
d
e
(
old_content = json_decode(
    oldc?ontent=jsond?ecode(this->attributes[‘size’],true);
 foreach ($extra as KaTeX parse error: Expected 'EOF', got '&' at position 8: key => &?val) {
 // 如果旧数据未删除,且当前不存在图片,则将旧图片路径添加进去
 if (isset( 
o
l
d
c
o
n
t
e
n
t
[
old_content[
    oldc?ontent[key]) && !isset($val[‘img’])) {
 $val[‘img’] =  
o
l
d
c
o
n
t
e
n
t
[
old_content[
    oldc?ontent[key][‘img’] ?? ‘’;
 }
 }
 }  
t
h
i
s
?
>
a
t
t
r
i
b
u
t
e
s
[
′
a
t
t
r
′
]
=
j
s
o
n
e
n
c
o
d
e
(
a
r
r
a
y
v
a
l
u
e
s
(
this->attributes['attr'] = json_encode(array_values(
    this?>attributes[′attr′]=jsone?ncode(arrayv?alues(extra));
 }