diff --git a/.gitignore b/.gitignore index 7b37592..998f123 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.lock .phpunit.result.cache .php-cs-fixer.cache +.idea/ \ No newline at end of file diff --git a/src/VirtualColumn.php b/src/VirtualColumn.php index e3cfec6..bbe2d7d 100644 --- a/src/VirtualColumn.php +++ b/src/VirtualColumn.php @@ -5,6 +5,7 @@ namespace Stancl\VirtualColumn; use Illuminate\Contracts\Encryption\DecryptException; +use Illuminate\Database\Eloquent\MissingAttributeException; use Illuminate\Support\Facades\Crypt; /** @@ -42,7 +43,13 @@ protected function decodeVirtualColumn(): void ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables ); - foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) { + try { + $data = $this->getAttribute(static::getDataColumn()) ?? []; + } catch (MissingAttributeException) { + return; + } + + foreach ($data as $key => $value) { $attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables); if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) { diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index bb33458..9290b34 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -128,6 +128,20 @@ public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_ $this->assertSame($encodedBar->bar, 'bar'); } + /** @test */ + public function decoding_works_with_strict_mode_enabled() { + FooModel::shouldBeStrict(); + + FooModel::create([ + 'id' => 1, + 'foo' => 'bar' + ]); + + $id = FooModel::query()->pluck('id')->first(); + + $this->assertSame(1, $id); + } + // maybe add an explicit test that the saving() and updating() listeners don't run twice? /** @test */