From 53526fd26319bffdc9eef3ef23de7f1f822dc4fb Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Thu, 23 Mar 2023 11:58:02 +0100 Subject: [PATCH] Properly check if attributes with AsEnumArrayObject or AsEnumCollection cast are dirty --- src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php | 4 ++++ tests/Database/DatabaseEloquentModelTest.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 2b1b7ca2998d..65d3377fa643 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -17,6 +17,8 @@ use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject; use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection; +use Illuminate\Database\Eloquent\Casts\AsEnumArrayObject; +use Illuminate\Database\Eloquent\Casts\AsEnumCollection; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\InvalidCastException; use Illuminate\Database\Eloquent\JsonEncodingException; @@ -2059,6 +2061,8 @@ public function originalIsEquivalent($key) $this->castAttribute($key, $original); } elseif ($this->isClassCastable($key) && in_array($this->getCasts()[$key], [AsArrayObject::class, AsCollection::class])) { return $this->fromJson($attribute) === $this->fromJson($original); + } elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsEnumArrayObject::class, AsEnumCollection::class])) { + return $this->fromJson($attribute) === $this->fromJson($original); } elseif ($this->isClassCastable($key) && $original !== null && in_array($this->getCasts()[$key], [AsEncryptedArrayObject::class, AsEncryptedCollection::class])) { return $this->fromEncryptedString($attribute) === $this->fromEncryptedString($original); } diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 3270e288c226..10469e12dee1 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -314,7 +314,7 @@ public function testDirtyOnEnumCollectionObject() { $model = new EloquentModelCastingStub; $model->setRawAttributes([ - 'asEnumCollectionAttribute' => json_encode(['draft', 'pending']), + 'asEnumCollectionAttribute' => '["draft", "pending"]', ]); $model->syncOriginal(); @@ -335,7 +335,7 @@ public function testDirtyOnEnumArrayObject() { $model = new EloquentModelCastingStub; $model->setRawAttributes([ - 'asEnumArrayObjectAttribute' => json_encode(['draft', 'pending']), + 'asEnumArrayObjectAttribute' => '["draft", "pending"]', ]); $model->syncOriginal();