czim / laravel-modelcomparer by czim

Performs a deep-nested comparison of a model's before/after state.
2,858
2
2
Package Data
Maintainer Username: czim
Maintainer Contact: coen.zimmerman@endeavour.nl (Coen Zimmerman)
Package Create Date: 2017-01-23
Package Last Update: 2022-11-01
Language: PHP
License: MIT
Last Refreshed: 2024-04-25 03:00:03
Package Statistics
Total Downloads: 2,858
Monthly Downloads: 64
Daily Downloads: 4
Total Stars: 2
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Laravel Model Comparer

Latest Version on Packagist Software License Build Status

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

Laravel | Package :----------------|:-------- 5.3 and older | 0.9 5.4 to 5.7 | 1.4 5.8 and up | 1.5

Install

Via Composer

$ composer require czim/laravel-modelcomparer

In your config/app.php config, add the service provider

    Czim\ModelComparer\ModelComparerServiceProvider::class,

Usage

  1. Initialize a comparer instance
  2. Before making changes, set the before state on the comparer by passing in the model
  3. Make your changes to anything related to the model
  4. 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

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.