From 09bdf523e0169d0355a48996950a5561cbd06276 Mon Sep 17 00:00:00 2001 From: Nick Potts Date: Tue, 9 Apr 2024 06:38:08 +0800 Subject: [PATCH] Early return in the listener if data column isn't required --- src/VirtualColumn.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/VirtualColumn.php b/src/VirtualColumn.php index bbe2d7d..ba3a78b 100644 --- a/src/VirtualColumn.php +++ b/src/VirtualColumn.php @@ -43,13 +43,7 @@ trait VirtualColumn ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables ); - try { - $data = $this->getAttribute(static::getDataColumn()) ?? []; - } catch (MissingAttributeException) { - return; - } - - foreach ($data as $key => $value) { + foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) { $attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables); if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) { @@ -114,6 +108,10 @@ trait VirtualColumn return [ 'retrieved' => [ function () { + if (($this->attributes[static::getDataColumn()] ?? null) === null) { + return; + } + // Always decode after model retrieval $this->dataEncoded = true;