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 be5e477
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
14 changes: 10 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->diffJson($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->diffJson($attribute, $original);
} elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsEnumArrayObject::class, AsEnumCollection::class])) {
return $this->fromJson($attribute) === $this->fromJson($original);
return $this->diffJson($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) {
return $this->diffJson($attribute, $original);
}

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

private function diffJson($attribute, $original)
{
return !count(array_diff_assoc_recursive($this->fromJson($attribute), $this->fromJson($original)));
}
}
33 changes: 33 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,36 @@ function with($value, callable $callback = null)
return is_null($callback) ? $value : $callback($value);
}
}

if (!function_exists('array_diff_assoc_recursive')) {
/**
* Computes the difference between two arrays with additional index check recursively
*
* @link https://gist.github.com/tulik/30550e91c641c9a7564a407b691983ad
*
* @param mixed $array1
* @param mixed $array2
* @return array
*/
function array_diff_assoc_recursive($array1, $array2)
{
$difference = array();

foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (!isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if (!empty($new_diff)) {
$difference[$key] = $new_diff;
}
}
} elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}

return $difference;
}
}

0 comments on commit be5e477

Please sign in to comment.