$fields */ public function fill(Model $subject, array $fields, ?User $actor = null): void { foreach ($fields as $field => $value) { $next = is_string($value) ? trim($value) : $value; $next = $next === '' ? null : $next; $before = $subject->getAttribute($field); $before = $before === '' ? null : $before; if ($before === $next) { continue; } $subject->setAttribute($field, $next); IdentityChange::query()->create([ 'subject_type' => $subject->getMorphClass(), 'subject_id' => $subject->getKey(), 'field' => $field, 'before' => $before, 'after' => $next, 'actor_id' => $actor?->id, 'occurred_at' => now(), ]); } $subject->save(); } }