Skip to content

Instantly share code, notes, and snippets.

@Braunson
Created December 2, 2020 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Braunson/3ce5d123f85b747835f5214225767fdf to your computer and use it in GitHub Desktop.
Save Braunson/3ce5d123f85b747835f5214225767fdf to your computer and use it in GitHub Desktop.
Laravel - Case your dirty attributes the same way they would be if they were using `getOriginal()`. Add this trait to your app and import it into the model you want to use it with.
<?php
namespace App\Traits;
trait DirtyOverride
{
/**
* Get the attributes that have been changed since last sync.
*
* @return array
*/
public function getDirty()
{
$dirty = [];
foreach ($this->getAttributes() as $key => $value) {
if (! $this->originalIsEquivalent($key)) {
$dirty[$key] = $this->transformModelValue(
$key, $value
);
}
}
return $dirty;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment