From 92b833a428f0b66998ebfd4fbaea3e4b6c3045d2 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 585071a35d39..ce4882fc8518 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; @@ -2042,6 +2044,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 543c922fc7e2..a720c224a3a4 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();