getDirty() as $attribute => $value) { //only store updates of non guarded stuff, i.e. attributes that are mass assignable if (!in_array($attribute, $object->guarded)) { //get the old value $original = $object->getOriginal($attribute); // skip trivial changes if ($original !== $object->$attribute) { $changes[] = [ 'object_type' => self::class, 'object_id' => $object->id, 'attribute' => $attribute, 'old_value' => $original, 'new_value' => $object->$attribute, // leave this out if you like normalized tables 'actor_id' => Auth::check() ? Auth::user()->id : null, 'created_at' => $now, 'updated_at' => $now, ]; } } } // bulk insert Change::insert($changes); }); } public function changes() { return $this->hasMany(Change::class, 'object_id')->where('object_type', static::class); } }