laravel-admin
laravel-admin copied to clipboard
JSON格式字段处理之BUG
- Laravel Version: 5.8.x
- PHP Version: 7.1.x
- Laravel-admin: 1.8.x
Description:
文档地址:https://www.laravel-admin.org/docs/zh/1.x/model-form-json-fields
问题描述:当模型字段是一对一关系的字段时,比如:users 表和profiles表是一对一关联,取字段 profile.age
时,因为字段中包含了“.”,它和JS语法中的class的“.”冲突了,从而导致json组件中的【新增】、【移除】的按钮不起作用了
已找到替代方案
users模型追加一个不存在的字段
class User extends Model
{
protected $appends = ['column_name'];
}
访问器 和 修改器
public function getColumnNameAttribute()
{
$value = '从关联表里获取数据';
return array_values(json_decode($value, true) ?: []);
}
public function setColumnNameAttribute($value)
{
$value = json_encode(array_values($value));
//把$value保存到对应关联表
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
遇到 这样的问题:
编辑时 新增 的 下标会冲突...
模型设置需要这样处理:$this->attributes['matching_profile'] = json_encode(array_values($value));
完美解决...