1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 16:14:03 +00:00

Handle custom castables in VirtualColumn

This commit is contained in:
lukinovec 2023-08-08 15:50:55 +02:00
parent de9d9d7cab
commit b0e454e110

View file

@ -37,7 +37,12 @@ trait VirtualColumn
$defaultEncryptedCastables = ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'];
foreach ($model->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
if ($model->hasCast($key, array_merge($defaultEncryptedCastables, static::$customEncryptedCastables)) && static::valueEncrypted($value)) {
$keyHasEncryptedCastable = $model->hasCast($key, array_merge(
$defaultEncryptedCastables,
array_map(fn (string $castable) => strtolower($castable), static::$customEncryptedCastables) // hasCast() only works correctly with lowercase strings
));
if ($keyHasEncryptedCastable && static::valueEncrypted($value)) {
$model->attributes[$key] = $value;
} else {
$model->setAttribute($key, $value);