Skip to content

Commit bd70973

Browse files
committed
Don't break with Model::shouldBeStrict()` enabled
1 parent 7371aac commit bd70973

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
composer.lock
33
.phpunit.result.cache
44
.php-cs-fixer.cache
5+
.idea/

src/VirtualColumn.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Stancl\VirtualColumn;
66

77
use Illuminate\Contracts\Encryption\DecryptException;
8+
use Illuminate\Database\Eloquent\MissingAttributeException;
89
use Illuminate\Support\Facades\Crypt;
910

1011
/**
@@ -42,7 +43,13 @@ protected function decodeVirtualColumn(): void
4243
['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables
4344
);
4445

45-
foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
46+
try {
47+
$data = $this->getAttribute(static::getDataColumn()) ?? [];
48+
} catch (MissingAttributeException) {
49+
return;
50+
}
51+
52+
foreach ($data as $key => $value) {
4653
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);
4754

4855
if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {

tests/VirtualColumnTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_
128128
$this->assertSame($encodedBar->bar, 'bar');
129129
}
130130

131+
/** @test */
132+
public function decoding_with_works_with_strict_mode_enabled() {
133+
FooModel::shouldBeStrict();
134+
135+
FooModel::create([
136+
'id' => 1,
137+
'foo' => 'bar'
138+
]);
139+
140+
$id = FooModel::query()->pluck('id')->first();
141+
142+
$this->assertSame(1, $id);
143+
}
144+
131145
// maybe add an explicit test that the saving() and updating() listeners don't run twice?
132146
133147
/** @test */

0 commit comments

Comments
 (0)