spatie/laravel-activitylog is one of those packages you install once and forget about. It hooks into your models, records who did what and when, and gives you a clean activity_log table you can query whenever someone asks "what happened here?"
It's a great answer to "do we have an audit trail?" It's not an answer to a harder question: can you prove that trail hasn't been altered?
Anyone with write access to your database (a DBA doing a "quick fix", a bad migration, a compromised admin account, an incident responder cleaning up after themselves) can open the activity_log table and edit a row. Change a causer_id. Rewrite a description. Delete the one entry that's inconvenient. Nothing in a default Activity Log setup will tell you it happened.
For most apps, that's an acceptable risk. For a system where the log itself carries legal or contractual weight (who approved this, who was notified, when a threshold was crossed and by whom), "we have a log" isn't good enough. You need to be able to say "this log has not been tampered with since it was written," and back that up.
Here's how I added that guarantee on top of Activity Log, using nothing more exotic than hash_hmac() and a model observer.
Why I added this
To be precise about scope: this isn't a log of every system event. It's attached to a handful of specific business models via Activity Log's LogsActivity trait: the records whose creation, modification, or deletion carries administrative or legal weight, timestamped and attributable to the user who made the change. Background jobs, scheduled recomputations, and internal state transitions don't go through this path. The goal was never "log everything," it was accountability for the human-initiated changes that matter: someone created this record, someone edited this field, someone deleted this. That's the record that would get pulled up if a decision was ever challenged. That changes the bar. It's no longer "can we see what happened," it's "can we defend what happened."
The obvious options were heavier than the problem warranted: an append-only log shipped to a separate system, a write-once storage backend, a full blockchain-backed audit trail. All defensible, all overkill for a single Laravel app that just needed one property: if a row in activity_log is ever edited outside of the normal write path, that should be detectable. That's a much smaller problem, and it has a much smaller solution.
Why HMAC instead of a hash?
The naive fix is tempting: hash the row's contents, store the hash alongside it, and compare on read.
$fingerprint = hash('sha256', json_encode($payload));
This proves nothing. If an attacker can edit the row, they can also recompute hash('sha256', ...) over their edited version and overwrite the stored fingerprint to match. A plain hash only detects accidental corruption: bit rot, a bad UPDATE that missed a column. It does nothing against anyone who can read the code, because there's no secret involved: the function to "make this row look legitimate" is public.
What you actually need is a function that produces the right output only if you know something the attacker doesn't. That's exactly what HMAC gives you: a hash computed with a secret key, hash_hmac('sha256', $payload, $key). Without $key, you can edit the row, but you can't produce a signature that will pass verification. The signature stops being "what does this data look like" and becomes "prove that someone with the signing key produced this exact payload".