czim / laravel-modelcomparer
Performs a deep-nested comparison of a model's before/after state.
Installs: 3 356
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
Requires (Dev)
- czim/laravel-paperclip: *
- czim/phpunit-printer: ^1.0
- mockery/mockery: ^1.0
- nunomaduro/larastan: ^2.2
- orchestra/database: ^7.0
- orchestra/testbench: ^7.0
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-10-29 04:04:09 UTC
README
This model comparer is a tool to make it easy to collect, log and report changes made to Eloquent models and their relations.
It's easy enough to compare model attributes before and after (or using getDirty()
, during) updates for a single Eloquent model.
Unfortunately, it is arduous to track updates while updating deeply nested relational model structures.
With this package, it's as simple as loading in the model before making changes, then loading it in again after. The comparer instance tracks the changes and offers the means to create concise changelogs.
Version Compatibility
Install
Via Composer
$ composer require czim/laravel-modelcomparer
If you're not using auto-discovery (or are installing a version older than 3.0),
add the service provider in your config/app.php
config.
Czim\ModelComparer\ModelComparerServiceProvider::class,
Usage
- Initialize a comparer instance
- Before making changes, set the before state on the comparer by passing in the model
- Make your changes to anything related to the model
- Run the comparison by passing in the model again
The result is an comparison information object that stores all the changes and offers means for easy logging.
To Do
-
Add singleton with facade for easy tracking of changes
- would use the model's class & key to keep track of before states and allow setting after states
- might even be done using an observer pattern
- note: not recommended for long running processes, unless cleanup methods are used to keep memory load small
-
Better change tracking for related models
- Track before state index by model class & key in the comparer
- And, singleton should delegate this to currently active comparers
- Track changes of a model when it is still via-via related before AND after (but for a different intermediary relation)
- Allow manually 'loading in' before state for a to-be related model
- Track before state index by model class & key in the comparer
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.