Tuesday, November 24, 2026SubscribeMastheadAbout
Cali ReporterCalifornia · National · West Coast · Good News

Advertisement

Technology

Implementing Automatic Audit Trails in Laravel Applications

This article details how to implement an automatic audit trail system in Laravel applications using a custom trait. The system logs all changes to model data, providing traceability for every create, update, and delete…

Portrait of Benjamin Boyce
Artificial Intelligence & Technology · November 19, 2026
Share

AI-generated editorial illustration from an imagined camera angle.
AI-generated editorial illustration from an imagined camera angle. Source image: dev.to.

An audit trail is essential in production applications to track changes made to data records. According to the Dev.to article, without an audit trail, it is difficult to answer user inquiries such as 'My order status was changed without my approval.' With an audit trail, it is possible to trace changes back to a specific user, time, and value change. The ModelChangeLogger trait is used in projects to automatically log every create, update, or delete action for models like ServiceRequest, Task, Enrolment, Order, and Partner. This trait is added to models, and it automatically records changes without requiring additional setup (Dev.to, 1). [1]

The implementation begins with creating a polymorphic database table called model_change_logs, which stores information about the change, including the model type, model ID, change type (created, updated, deleted), the actual changes, and a request ID for traceability. The ModelChangeLog model is also defined, with attributes such as model_type, model_id, change_type, changes, request_id, user_id, and user_type. The changes are stored as a JSON array, and the model relationship is defined using Laravel's morphTo method (Dev.to, 1). [1]

The ModelChangeLogger trait is used to automatically log changes when a model is created, updated, or deleted. It uses Laravel's Eloquent events to hook into these actions and record the changes. The trait includes a logChanges method that stores the change details, such as the user who made the change, the type of change, and the request ID. Additionally, the trait resolves the actor by checking the authentication guards (admin, merchandiser, web, etc.) to determine which user made the change (Dev.to, 1). [1]

An audit trail answers: who changed what, when, and from what value to what value. In the projects here, every model that processes business data – ServiceRequest, Task, Enrolment, Order, Partner – uses the ModelChangeLogger trait. It’s automatic: you add the trait, and every create/update/delete is recorded. The database table is designed as a polymorphic table, allowing it to store changes for any model type (Dev.to, 2023). [1]

Advertisement

The model_change_logs table includes fields such as model_type, model_id, change_type, changes, request_id, user_id, and user_type. The changes field stores the actual diff in JSON format, while the request_id ties change logs to HTTP requests for full traceability (Dev.to, 2023). [1]

The ModelChangeLogger trait logs changes automatically by hooking into Laravel’s Eloquent events. It uses the getOriginal() and getAttributes() methods to detect and record only the fields that changed, along with old and new values. This trait also resolves the actor (user) across all guards (admin, merchandiser, web, etc.) and stores the user type and ID associated with the change (Dev.to, 2023). [1]

An audit trail answers: who changed what, when, and from what value to what value. The model_change_logs table includes fields such as model_type, model_id, change_type, changes, request_id, user_id, and user_type. The ModelChangeLogger trait logs changes automatically by hooking into Laravel’s Eloquent events. This approach ensures that every modification to a model is recorded without requiring additional configuration for each model. The trait is designed to be bootable, meaning that adding it to a model automatically enables logging for create, update, and delete operations. [1]

The ModelChangeLogger trait uses Laravel’s Eloquent events to detect changes and log them. When a model is created, updated, or deleted, the trait captures the relevant data and stores it in the model_change_logs table. This includes the model type, model ID, change type (such as 'created' or 'updated'), a JSON representation of the changes made, and a request ID for traceability. The changes are stored with old and new values for each attribute that was modified, ensuring that users can track exactly what was changed and by whom. [1]

Advertisement

The model_change_logs table is polymorphic, allowing it to store change logs for multiple model types in a single table. This design simplifies querying and reduces redundancy. The trait also resolves the user who made the change by checking all defined authentication guards, such as 'admin', 'merchandiser', or 'web', and stores the user ID and type in the log. This information is crucial for accountability and auditing purposes. Additionally, the request ID from the HTTP request is attached to each log entry, enabling full-stack traceability from the request to the model changes it caused. [1]

Sources

Dev.to

Prepared with AI assistance from the cited sources.

The Cali Reporter newsletter

The stories worth your time.

California, the country, and what comes next. Join our free newsletter for reporting that keeps you informed.

By joining, you agree to receive the Cali Reporter newsletter. Unsubscribe at any time. Privacy policy.

More in Technology

  • Modern Web Guidance 1.0 and Browser Compatibility

    Modern Web Guidance 1.0, announced by Chrome for Developers on November 10, introduces browser compatibility data to assist coding agents in building web features. The initiative emphasizes the importance of fallback…

  • Apache mod_http2 Vulnerability Detection with Nmap

    A new Nmap scripting module has been developed to detect the Apache mod_http2 vulnerability (CVE-2026-119618), addressing the challenges of identifying affected systems accurately.

  • Securing Remote Vendor Access in Industrial Networks

    Best practices for managing remote vendor access in operational technology (OT) networks emphasize controlled access points, multifactor authentication, and time-bound sessions to reduce cybersecurity risks.

Advertisement