Laravel Auditing
repository·master·Indexed 25 days ago
https://github.com/owen-it/laravel-auditingA Laravel package for tracking changes to Eloquent models using a trait to maintain a history of modifications for security and business auditing. It supports various audit event types (retrieved, created, updated, deleted, restored), queued auditing for performance, and customizable audit drivers and resolvers. The package includes an Auditor facade and an Audit model to manage and access audit records, including old and new values, associated users, and timestamps.
What's inside laravel-auditing
- Laravel Auditing is a package designed to help you track and understand changes in your Eloquent models. It provides a history of model changes by using a trait, allowing you to identify discrepancies, anomalies, or suspect activities within your application data. Once auditing is enabled, retrieving the history of changes is straightforward for display or analysis.
Publish the auditing configuration and migrations
masterTo customize the package behavior or set up the database schema, you can publish the configuration file and the migration stub using the Laravel
vendor:publishcommand.Available tags:
config: Publishes theconfig/audit.phpfile.migrations: Publishes theaudits.stubmigration file to yourdatabase/migrationsdirectory.
Install Laravel Auditing via artisan command
masterRun the
auditing:installcommand to automate the initial setup of the package. This command performs the following actions:- Publishes the Auditing configuration file.
- Publishes the Auditing database migrations.
- Registers the
OwenIt\Auditing\AuditingServiceProvider::classin your application'sconfig/app.phpfile.
Configure queued auditing via audit.queue.enable
masterThe auditing process can be offloaded to a queue to improve performance. This is controlled by the
audit.queue.enableconfiguration key.- If
audit.queue.enableisfalse(default), audits are executed synchronously usingAuditor::execute($model). - If
audit.queue.enableistrue, the observer will dispatch aDispatchAuditevent, allowing the audit to be handled asynchronously.
- If
Configure empty value auditing behavior
masterThe
Auditorclass respects two configuration keys to determine if audits should be recorded when no changes are detected:audit.empty_values: A boolean determining if empty changes should be stored.audit.allowed_empty_values: An array of event names (e.g., fromgetAuditEvent()) that are explicitly allowed to store empty values even ifaudit.empty_valuesis false.
If both conditions are not met and both
new_valuesandold_valuesare empty, the audit process will exit without creating a record.Disable auditing via configuration
masterThe package checks theaudit.enabledconfiguration key during the boot process. Ifaudit.enabledis set tofalsein yourconfig/audit.phpfile, the auditing event listeners will not be registered, effectively disabling the auditing functionality.Check Laravel Auditing version compatibility
masterBefore installing, ensure your environment meets the requirements for the version you intend to use. The current active support version is 14.x.
Version Illuminate Status PHP Version 14.x 11.x.x - 13.x.x Active support >= 8.2 13.x 7.x.x - 11.x.x End of life >= 7.3 | 8.0 12.x 6.x.x - 9.x.x End of life >= 7.3 | 8.0 11.x 5.8.x - 8.x.x End of life >= 7.3 10.x 5.8.x - 7.x.x End of life >= 7.2.5 9.x 5.8.x - 6.x.x End of life >= 7.1.3 8.x 5.2.x - 5.7.x End of life >= 7.0.13 7.x 5.2.x - 5.6.x End of life >= 7.0.13 6.x 5.2.x - 5.6.x End of life >= 7.0.13 5.x 5.2.x - 5.5.x End of life >= 7.0.13 4.x 5.2.x - 5.5.x End of life >= 5.5.9 3.x 5.2.x - 5.4.x End of life >= 5.5.9 2.x 5.1.x - 5.3.x End of life >= 5.5.9 Execute auditing for a model
masterThe
executemethod triggers the auditing process for a givenAuditablemodel. It performs the following steps:- Checks if the model is
readyForAuditing(). - Resolves the appropriate
AuditDriver. - Fires the
Auditingevent (which can be used to cancel the audit). - Evaluates whether to skip auditing based on the
audit.empty_valuesandaudit.allowed_empty_valuesconfiguration settings if no changes are detected. - Instructs the driver to
audit($model). - Calls
$driver->prune($model)to clean up old audit data. - Dispatches the
Auditedevent upon successful completion.
- Checks if the model is
Use the Auditor facade to manage audits
masterTheAuditorfacade provides a static interface to the auditing service. You can use it to retrieve the audit driver for a specific model or to manually trigger the execution of an audit for a model.Retrieve the audit driver for a model
masterUse theauditDrivermethod to obtain the specificAuditDriverinstance assigned to anAuditablemodel. This is useful when you need to interact directly with the driver responsible for storing or managing that model's audit logs. The driver is determined by the value returned from the model'sgetAuditDriver()method.Audit event types for Auditable models
masterThe
AuditableObserverautomatically triggers audits based on Eloquent model lifecycle events. When an audit is dispatched, the model's audit event type is set to one of the following values:retrievedcreatedupdateddeletedrestored
Note: During a model restoration process, the observer tracks the
restoringstate to prevent duplicate or incorrectupdatedaudits from being generated alongside therestoredaudit.Access audit record properties
masterThe
Auditmodel represents a single audit entry. When interacting with an instance of this model, you can access the following properties:event: The type of event that triggered the audit (e.g.,created,updated).tags: A string containing tags associated with the audit.new_values: An array of the values after the change.old_values: An array of the values before the change.auditable_type: The class name of the model being audited.auditable_id: The primary key of the model being audited.user: The user responsible for the change (implementsAuthenticatable).auditable: The actual model instance that was audited (implementsAuditable).created_at/updated_at: Timestamps for the audit record.