1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 21:34:05 +00:00

Refactor encodeAttributes()

This commit is contained in:
lukinovec 2023-08-14 16:42:14 +02:00
parent daeb8d8c86
commit e411001251

View file

@ -60,18 +60,21 @@ trait VirtualColumn
return; return;
} }
foreach ($model->getAttributes() as $key => $value) { $dataColumn = static::getDataColumn();
if (! in_array($key, static::getCustomColumns())) { $customColumns = static::getCustomColumns();
$current = $model->getAttribute(static::getDataColumn()) ?? []; $attributes = array_filter($model->getAttributes(), fn ($key) => ! in_array($key, $customColumns), ARRAY_FILTER_USE_KEY);
$model->setAttribute(static::getDataColumn(), array_merge($current, [ // Remove data column from the attributes
$key => $value, unset($attributes[$dataColumn]);
]));
foreach ($attributes as $key => $value) {
// Remove attribute from the model
unset($model->attributes[$key]); unset($model->attributes[$key]);
unset($model->original[$key]); unset($model->original[$key]);
} }
}
// Add attribute to the data column
$model->setAttribute($dataColumn, $attributes);
$model->dataEncoded = true; $model->dataEncoded = true;
} }