ensi / laravel-auditing
laravel auditing
Installs: 20 371
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 3
Open Issues: 1
Requires
- php: ^8.1
- laravel/framework: ^9.0 || ^10.0 || ^11.0
- ramsey/uuid: ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- nunomaduro/collision: ^6.0 || ^7.0 || ^8.1
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- pestphp/pest: ^1.22 || ^2.0
- pestphp/pest-plugin-laravel: ^1.1 || ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.11
- spaze/phpstan-disallowed-calls: ^2.15
README
Opiniated fork of owen-it/laravel-auditing
Installation
You can install the package via composer:
composer require ensi/laravel-auditing
Publish the migrations with:
php artisan vendor:publish --provider="Ensi\LaravelAuditing\LaravelAuditingServiceProvider"
Migrate from 0.2.x to 0.3.0
- Publish new migration
php artisan vendor:publish --provider="Ensi\LaravelAuditing\LaravelAuditingServiceProvider" --tag=migrations-0.3
- If the config
laravel-auditing.php
is published, then replace theresolver.user
value withEnsi\LaravelAuditing\Resolvers\UserResolver::class
Version Compatibility
Basic Usage
By default, no modification history is saved for models.
To enable logging for a specific model, you need to add the Support s Audit
trait and the Auditable
interface to it
use Ensi\LaravelAuditing\Contracts\Auditable; use Ensi\LaravelAuditing\SupportsAudit; class Something extends Model implements Auditable { use SupportsAudit; }
If we change the data of the child models from a logical point of view and want this change to take place under the parent model in the history, it is necessary to set the root entity (i.e. the model) in the transaction before changing the data.
This is done through the Transaction
facade or the manager \\Ensi\\LaravelAuditing\\Transactions\\ExtendedTransactionManager
DB::transaction(function () { Transaction::setRootEntity($rootModel); $relatedModel->save(); });
To add data to the history about who made the changes (a specific user, or, for example, a console command), again, you need to do this before changing the data, but through the Subject
facade or the injection of \\Ensi\\LaravelAuditing\\Resolvers\\SubjectManager
Subject::attach($subject); // $subject - объект реализующий Ensi\LaravelAuditing\Contracts
The subject does not unbind after the transaction is completed.
It can be unlinked manually by calling the Subject::detach()
method.
When processing http requests, you can set the subject in middleware. In console commands and handlers, event queues are reassigned during execution.
The subject can be any entity that supports the interface \Ensi\LaravelAuditing\Contracts\Principal
.
If the subject is an ongoing task, for example, importing from a file, then it can return the ID of the user who created the task in the getUserIdentifier()
method, and return the name of the imported file as the name.
In the user model, the getAuthIdentifier()
and getUserIdentifier()
methods return the same identifier.
Also, unlike the original package, not only the changed fields are saved in the history, but also the complete state of the model object at the time of the change.
Contributing
Please see CONTRIBUTING for details.
Testing
- composer install
- composer test
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.