Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add date and cast check for dirty #18385

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,11 +960,8 @@ public function getDirty()
{
$dirty = [];

foreach ($this->attributes as $key => $value) {
if (! array_key_exists($key, $this->original)) {
$dirty[$key] = $value;
} elseif ($value !== $this->original[$key] &&
! $this->originalIsNumericallyEquivalent($key)) {
foreach ($this->getAttributes() as $key => $value) {
if (! $this->originalIsEquivalent($key, $value)) {
$dirty[$key] = $value;
}
}
Expand All @@ -973,16 +970,29 @@ public function getDirty()
}

/**
* Determine if the new and old values for a given key are numerically equivalent.
* Determine if the new and old values for a given key are equivalent.
*
* @param string $key
* @param string $key
* @param mixed $current
* @return bool
*/
protected function originalIsNumericallyEquivalent($key)
protected function originalIsEquivalent($key, $current)
{
$current = $this->attributes[$key];
if (! array_key_exists($key, $this->original)) {
return false;
}

if ($current === $original = $this->getOriginal($key)) {
return true;
}

$original = $this->original[$key];
if ($this->isDateAttribute($key)) {
return $this->fromDateTime($current) === $this->fromDateTime($original);
}

if ($this->hasCast($key)) {
return $this->castAttribute($key, $current) === $this->castAttribute($key, $original);
}

// This method checks if the two values are numerically equivalent even if they
// are different types. This is in case the two values are not the same type
Expand Down