Performs a deep-nested comparison of a model's before/after state.

3.0.0 2022-11-01 16:36 UTC


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 PHP Version
5.3 and older 0.9
5.4 to 5.7 1.4
5.8 1.5
5.8, 6.0+ 2.0
9.0+ 3.0 8.1+


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.



  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


Please see CONTRIBUTING for details.



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