Skip to content

Instantly share code, notes, and snippets.

Created May 11, 2018 09:34
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Fixes the cast dirty problems in < Laravel 5.5 projects. Tested with L5.3
// TODO: remove when bug is fixed,
// Copied from the change that fixed it in 5.5:
trait EloquentDirtyFix
public function getDirty()
$dirty = [];
foreach ($this->getAttributes() as $key => $value) {
if (! $this->originalIsEquivalent($key, $value)) {
$dirty[$key] = $value;
return $dirty;
protected function originalIsEquivalent($key, $current)
if (! array_key_exists($key, $this->original)) {
return false;
if ($current === $original = $this->getOriginal($key)) {
return true;
// When check rich this check and current attribute value not equals with original, we should skip next steps
// if current is null
if (is_null($current) || is_null($original)) {
return false;
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
// we can do a fair comparison of the two values to know if this is dirty.
return is_numeric($current) && is_numeric($original)
&& strcmp((string) $current, (string) $original) === 0;
protected function isDateAttribute($key)
return in_array($key, $this->getDates()) ||
Copy link


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment