惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Recent Announcements
Recent Announcements
D
Docker
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC
B
Blog
博客园_首页
罗磊的独立博客
D
DataBreaches.Net
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Efficient Auditing: Mastering PostgreSQL JSONB in Laravel 🛡️
Prajapati Pa · 2026-05-15 · via DEV Community

The Problem with Polymorphic Audit Logs

In B2B SaaS engineering at Smart Tech Devs, maintaining a robust audit trail—tracking exactly who changed what, and when—is often a hard compliance requirement. The standard Laravel approach involves using polymorphic relations to a central audits table, creating a new row for every single model update.

When your platform scales to millions of rows in core tables like invoices or users, this auditing strategy collapses. Your central audits table becomes massive, indexing it chokes your database, and prunning old data becomes an expensive locking operation. Furthermore, standard logging often captures the entire model state rather than just the changeset, wasting precious disk space. To build durable architecture, we must utilize the power of **PostgreSQL JSONB**.

Enter JSONB compact Changesets

PostgreSQL's JSONB data type allows us to store unstructured, indexed JSON data efficiently. Instead of creating massive polymorphic log tables, we append a compact "changeset" directly to a dedicated audit column on the main model, or into a sidecar table using JSONB.

Step 1: The Database Migration

Instead of a separate table, we add a jsonb column to the model we want to audit. We also ensure a GIN index is created on this column for fast lookups.


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddAuditLogToInvoicesTable extends Migration
{
    public function up(): void
    {
        Schema::table('invoices', function (Blueprint $table) {
            // Store a compact array of changesets: [{user_id, changed_at, old: {}, new: {}}, ...]
            $table->jsonb('audit_log')->nullable();
        });

        // Add a GIN index for efficient querying inside the JSONB data
        DB::statement('CREATE INDEX invoices_audit_log_gin ON invoices USING GIN (audit_log);');
    }
}

Step 2: Efficient Auditing via Model Observers

We do not capture the whole model. We use an Eloquent Observer to hook into the updating event, calculate the strict changeset using Laravel's getDirty() and getOriginal() methods, and prepend the minimal JSON delta to our audit column.


namespace App\Observers;

use App\Models\Invoice;
use Illuminate\Support\Facades\Auth;

class InvoiceObserver
{
    /**
     * Handle the Invoice "updating" event.
     */
    public function updating(Invoice $invoice): void
    {
        // 1. Get ONLY the changed attributes and their original values
        $dirty = $invoice->getDirty();
        $original = array_intersect_key($invoice->getOriginal(), $dirty);

        if (empty($dirty)) {
            return; // No changes made
        }

        // 2. Create a compact changeset
        $changeset = [
            'user_id' => Auth::id() ?? 'system',
            'changed_at' => now()->toIso8601String(),
            'old' => $original,
            'new' => $dirty,
        ];

        // 3. Prepend the changeset to the existing JSONB array (using database-level concatenation)
        // We do this to avoid loading the entire audit history into memory just to append.
        $invoice->audit_log = DB::raw("jsonb_insert(COALESCE(audit_log, '[]'), '{0}', '" . json_encode($changeset) . "')");
    }
}

The Engineering ROI

Transitioning to JSONB changesets on the model itself (or a sidecar table) fundamentally upgrades your auditing infrastructure:

  • Drastically Reduced Database Bloat: You are only storing the data that *changed*, rather than duplicating the entire model state multiple times.
  • Flat Query Performance: Fetching a model's audit history is now a single, indexed lookup on that model's row, rather than a complex join on a multi-million-row polymorphic table.
  • Simplified Pruning: If you need to purge data older than 2 years, you don't need expensive locked deletes on a central table. You can run simple, partitioned database updates or simply ignore older entries inside the JSONB structure.

Conclusion

Polymorphic audit logs are a liability at scale. By leveraging the flexibility and performance of PostgreSQL's JSONB inside your Laravel Observers, you build an immutable, highly efficient audit trail that safeguards data integrity without compromising system performance.