Download the PHP package philiprehberger/laravel-model-diff without Composer
On this page you can find all versions of the php package philiprehberger/laravel-model-diff. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download philiprehberger/laravel-model-diff
More information about philiprehberger/laravel-model-diff
Files in philiprehberger/laravel-model-diff
Package laravel-model-diff
Short Description Track and display structured differences between Eloquent model versions with human-readable labels
License MIT
Homepage https://github.com/philiprehberger/laravel-model-diff
Informations about the package laravel-model-diff
Laravel Model Diff
Track and display structured differences between Eloquent model versions with human-readable labels.
Requirements
- PHP 8.2+
- Laravel 11 or 12
Installation
Install via Composer:
The service provider and facade are registered automatically via Laravel package auto-discovery.
Publishing the config
This creates config/model-diff.php in your application.
Configuration
Usage
Comparing two model instances
Pass two instances of the same model — a "before" snapshot and an "after" snapshot — to ModelDiff::compare():
Comparing an unsaved dirty model
Use ModelDiff::fromDirty() to inspect changes on a model that has not yet been saved:
Excluding extra attributes at call-site
Filtering results
Use only() and except() to narrow a DiffResult after comparison:
Accessing individual values
Retrieve the old or new value of a single attribute without iterating:
Human-Readable Labels
Using the HasDiffLabels trait
Add the HasDiffLabels trait to any model and define a $diffLabels map:
Attributes without an explicit entry are automatically humanized:
billing_address becomes Billing Address.
Retrieving a label directly
API
DiffResult
| Method | Return type | Description |
|---|---|---|
hasChanges() |
bool |
true when at least one attribute changed |
changedAttributes() |
string[] |
Names of changed attributes |
getChanges() |
AttributeChange[] |
All change objects |
only(string[] $attributes) |
DiffResult |
New result with only the specified attributes |
except(string[] $attributes) |
DiffResult |
New result excluding the specified attributes |
getBefore(string $attribute) |
mixed |
Old value for an attribute (null if not in diff) |
getAfter(string $attribute) |
mixed |
New value for an attribute (null if not in diff) |
toArray() |
array |
Plain array — one entry per change |
toHumanReadable() |
array |
Keyed by label; values formatted for display |
toArray() output
toHumanReadable() output
AttributeChange
| Property | Type | Description |
|---|---|---|
$attribute |
string |
Raw attribute name |
$old |
mixed |
Normalized old value |
$new |
mixed |
Normalized new value |
$label |
string |
Human-readable label |
Cast-Aware Comparison
The package normalizes values before comparing them, so you never get false positives from type mismatches:
| Cast type | Normalization |
|---|---|
date, datetime, immutable_date/datetime |
Parsed to Carbon and formatted with date_format config |
timestamp |
Parsed to Carbon and formatted with date_format config |
array, json, object, collection |
Decoded and compared by content, not by serialized string |
boolean, bool |
Strict (bool) cast before comparison |
integer, int |
Strict (int) cast |
float, double, real |
Strict (float) cast |
decimal:N |
Strict (float) cast |
Backed enum (SomeEnum::class) |
Compared by ->value; stored as scalar in AttributeChange |
Unit enum (SomeEnum::class without backing) |
Compared by ->name; stored as string in AttributeChange |
Note: Associative arrays are compared order-insensitively —
['a' => 1, 'b' => 2]equals['b' => 2, 'a' => 1]. Sequential (list) arrays are compared in order.
Using the Facade
The ModelDiff facade is registered automatically:
Using the Class Directly
If you prefer not to use the facade, resolve the class from the container or instantiate it directly:
Development
Support
If you find this project useful:
License
MIT
All versions of laravel-model-diff with dependencies
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0