Download the PHP package ernestoch/user-auditable-for-laravel without Composer
On this page you can find all versions of the php package ernestoch/user-auditable-for-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package user-auditable-for-laravel
User Auditable for Laravel
A Laravel package that provides user auditing capabilities for your database tables and Eloquent models. Easily track which users create, update, and delete records in your application.
Features
- ๐ต๏ธ User Auditing: Automatically track
created_by,updated_by, anddeleted_by - ๐ง Flexible Macros: Schema macros for easy migration creation
- ๐ฏ Multiple Key Types: Support for ID, UUID, and ULID
- ๐ท๏ธ Relationships: Built-in relationships to user models
- ๐ Query Scopes: Easy filtering by user actions
- ๐ญ Custom Events: Track any business event with dynamic
EventAuditabletrait - ๐งพ Change Tracking: Log model changes and revert to previous states with
ChangeAuditable - โก Zero Configuration: Works out of the box
Requirements
- PHP 8.1 or higher
- Laravel 9.0 or higher
Laravel 9 notice: Laravel 9 reached End of Life in February 2024 and carries known security advisories. This package declares compatibility with Laravel 9 but CI tests for that version may fail due to Composer blocking EOL packages. Use Laravel 9 at your own risk.
Installation
Configuration
Publish the configuration file (optional):
Note:
fullAuditable()requiresuser_auditableto also be listed inenabled_macros. If you disableuser_auditablein the published config, registeringfull_auditablewill throw aRuntimeExceptionat boot time.
Usage
Migrations
Use the provided macros in your migrations:
Custom Event Columns
Use eventAuditable() to stamp any custom business event with its own _at timestamp
and/or _by user FK, reading user_table and key_type from config('user-auditable.defaults'):
Reversing Migrations
All creation macros have corresponding drop macros for clean rollbacks:
Models
Use the UserAuditable trait in your Eloquent models:
Use the EventAuditable trait for dynamic access to custom events:
Relationships
The traits automatically provide relationships:
UserAuditable Relationships
EventAuditable Relationships
With EventAuditable trait, access relationships dynamically for any event:
Query Scopes
UserAuditable Scopes
Filter records by user actions:
EventAuditable Scopes
With EventAuditable trait, filter by any event user dynamically:
Change Tracking
ChangeAuditable automatically logs every create, update, delete, restore, and revert operation on your model to a dedicated audit_logs table.
Migration
Create the audit log table using the provided macro:
Model Setup
$hiddenmodel properties are also automatically excluded from audit logs.
Accessing Audit Logs
Reverting to a Previous State
revertTo() behavior:
- Reverts the same model instance (
$this) using the selected log'sold_values. - Creates a new audit entry with event
revertedafter a successful revert. - Avoids infinite audit loops by performing the revert update quietly.
- Throws
InvalidArgumentExceptionwhen:- The provided log does not belong to the current model (
auditable_type/auditable_idmismatch). - The provided log has no
old_valuesand therefore cannot be reverted.
- The provided log does not belong to the current model (
updated,deleted, andrevertedlogs are typically revertible (they includeold_values).
createdandrestoredlogs are typically not revertible because they do not includeold_values.
Diffing Between Two Logs
Pruning Old Logs
Configuration
Available Macros
| Macro | Description | Parameters |
|---|---|---|
| userAuditable() | Adds user auditing columns | ?string $userTable = null, ?string $keyType = null |
| dropUserAuditable() | Removes user auditing columns | bool $dropForeign = true |
| fullAuditable() | Adds timestamps, soft deletes, and user auditing | ?string $userTable = null, ?string $keyType = null |
| dropFullAuditable() | Removes timestamps, soft deletes, and user auditing | bool $dropForeign = true |
| uuidColumn() | Adds UUID column | string $columnName = 'uuid' |
| dropUuidColumn() | Removes UUID column | string $columnName = 'uuid' |
| ulidColumn() | Adds ULID column | string $columnName = 'ulid' |
| dropUlidColumn() | Removes ULID column | string $columnName = 'ulid' |
| statusColumn() | Adds status enum column | string $columnName = 'status', array $allowed = ['active','inactive','pending'], string $default = 'active' |
| dropStatusColumn() | Removes status column | string $columnName = 'status' |
| eventAuditable() | Adds a custom event timestamp and/or user FK | string $event, ?string $column = null |
| dropEventAuditable() | Removes custom event columns | string $event, ?string $column = null, bool $dropForeign = true |
| auditLogTable() | Creates the standard audit log table structure | no parameters |
| dropAuditLogTable() | Drops the current audit log table | no parameters |
Testing
Setup
A .env.testing.example file is included in the repository as a reference. Copy it and fill in your local values:
โ ๏ธ Never commit
.env.testingto the repository. It is already listed in.gitignore.
Running with MySQL
Set the following environment variables and fill in your values in the .env.testing file:
Then set DB_CONNECTION in your terminal session:
Running with SQLite (default)
No additional configuration is needed. SQLite in-memory is the default driver used by phpunit.xml.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
Author: Ernesto Chapon.
All contributors (โ ๏ธ Not available yet).
License
The MIT License (MIT). Please see License File for more information.