Skip to content

Commit

Permalink
Bugfix for issue laravel#46247, laravel#28029 and related
Browse files Browse the repository at this point in the history
  • Loading branch information
netzknecht committed Oct 27, 2023
1 parent 85a5146 commit 571636e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2071,8 +2071,7 @@ public function originalIsEquivalent($key)
return $this->fromDateTime($attribute) ===
$this->fromDateTime($original);
} elseif ($this->hasCast($key, ['object', 'collection'])) {
return $this->fromJson($attribute) ===
$this->fromJson($original);
return $this->jsonIsEquivalent($attribute, $original);
} elseif ($this->hasCast($key, ['real', 'float', 'double'])) {
if ($original === null) {
return false;
Expand All @@ -2083,11 +2082,13 @@ public function originalIsEquivalent($key)
return $this->castAttribute($key, $attribute) ===
$this->castAttribute($key, $original);
} elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsArrayObject::class, AsCollection::class])) {
return $this->fromJson($attribute) === $this->fromJson($original);
return $this->jsonIsEquivalent($attribute, $original);
} elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsEnumArrayObject::class, AsEnumCollection::class])) {
return $this->fromJson($attribute) === $this->fromJson($original);
return $this->jsonIsEquivalent($attribute, $original);
} elseif ($this->isClassCastable($key) && $original !== null && Str::startsWith($this->getCasts()[$key], [AsEncryptedArrayObject::class, AsEncryptedCollection::class])) {
return $this->fromEncryptedString($attribute) === $this->fromEncryptedString($original);
} elseif ($this->isClassCastable($key) && $this->resolveCasterClass($key) instanceof Json) {

This comment has been minimized.

Copy link
@bjugan

bjugan Oct 30, 2023

resolverCasterClass() method fails if attribute is casted to enum.

This comment has been minimized.

Copy link
@netzknecht

netzknecht Oct 31, 2023

Author Owner

Fixed with new commit: 33db4b8

return $this->jsonIsEquivalent($attribute, $original);
}

return is_numeric($attribute) && is_numeric($original)
Expand Down Expand Up @@ -2254,4 +2255,19 @@ protected static function getAttributeMarkedMutatorMethods($class)
return false;
})->map->name->values()->all();
}

/**
* Checks whether two Json strings are the same and, if not,
* whether the data decoded into an associative array is the same
* without taking the order or types of the elements into account.
*
* @param mixed $attribute
* @param mixed $original
* @return bool
*/
public function jsonIsEquivalent($attribute, $original)
{
return $original == $attribute
?: json_decode($attribute, true) == json_decode($original, true);
}
}

0 comments on commit 571636e

Please sign in to comment.