diff --git a/src/VirtualColumn.php b/src/VirtualColumn.php index e3cfec6..768a07f 100644 --- a/src/VirtualColumn.php +++ b/src/VirtualColumn.php @@ -45,7 +45,7 @@ protected function decodeVirtualColumn(): void foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) { $attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables); - if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) { + if ($value && $attributeHasEncryptedCastable && $this->valueEncrypted($value)) { $this->attributes[$key] = $value; } else { $this->setAttribute($key, $value); diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index bb33458..9808ad7 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -143,13 +143,16 @@ public function encrypted_casts_work_with_virtual_column() { 'json' => json_encode(['foo', 'bar']), // 'encrypted:json' 'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object' 'custom' => 'foo', // Custom castable – 'EncryptedCast::class' + 'null_value' => null, // 'encrypted' ]); foreach($encryptedAttributes as $key => $expectedValue) { $savedValue = $model->getAttributes()[$key]; // Encrypted - $this->assertTrue($model->valueEncrypted($savedValue)); - $this->assertNotEquals($expectedValue, $savedValue); + if ($savedValue !== null) { + $this->assertTrue($model->valueEncrypted($savedValue)); + $this->assertNotEquals($expectedValue, $savedValue); + } $retrievedValue = $model->$key; // Decrypted @@ -178,6 +181,7 @@ class MyModel extends ParentModel 'json' => 'encrypted:json', 'object' => 'encrypted:object', 'custom' => EncryptedCast::class, + 'null_value' => 'encrypted', ]; }