audunru / model-history
Log changes to Laravel models
Requires
- php: ^8.3
- laravel/framework: ^13.0
- spatie/laravel-package-tools: ^1.9
Requires (Dev)
- fakerphp/faker: ^1.12
- laravel/pint: ^1.0
- mockery/mockery: ^1.4
- orchestra/testbench: ^11.0
- php-coveralls/php-coveralls: ^2.2
- phpmd/phpmd: ^2.10
- phpunit/phpunit: ^12.0
- roave/security-advisories: dev-latest
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v13.x-dev
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.0
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.1
- v1.0.0
- v0.3.0
- v0.2.0
- v0.1.0
- dev-remove-lock
- dev-release-please--branches--master
- dev-release-please--branches--master--components--model-history
This package is auto-updated.
Last update: 2026-10-19 18:44:02 UTC
README
Keep a record of changes to models in your application. If a user changes the name of a product from A to B, that change will be stored in a Change model and stored in your database. Only the changed attributes are stored. You can then use this to retrieve the model's history, including which user made the change.
Installation
Step 1: Install with Composer
composer require audunru/model-history
Step 2: Publish and run migrations
Note: Changes are stored in a table called history. You can change this in the configuration, but you will have to publish the configuration before publishing and running the migrations.
php artisan vendor:publish --tag=model-history-migrations php artisan migrate
Step 3: Add traits to your models
Add the MakesChanges trait to your User model:
namespace App\Models; use audunru\ModelHistory\Traits\MakesChanges; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use MakesChanges;
Add the HasHistory trait to any model where you want to track changes:
namespace App\Models; use audunru\ModelHistory\Traits\HasHistory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasHistory;
Step 4: Retrieve model changes
Assuming that you've added the HasHistory trait to a model named Product, you can retrieve the changes like this:
$product = Product::create([ 'description' => 'Old description', ]); $product->update([ 'description' => 'New description', ]); dump($product->changes);
Configuration
Publish the configuration file by running:
php artisan vendor:publish --tag=model-history-config
Available options:
/* * Table where the "Change" model will be stored */ 'history_table_name' => 'history', /* * Eager load the change model's owner */ 'eager_load_owner' => true, /* * Eager load the change model's model */ 'eager_load_model' => false, /* * Date format used when returning the Change model as JSON */ 'date_format' => 'Y-m-d H:i:s', /* * Attribute names that are never recorded in history, regardless of each model's own $ignored * list. Override in your published config to add or remove entries. */ 'default_ignored' => [ 'password', 'remember_token', 'api_token', ],
Ignoring attributes
Changes to the attributes in the default_ignored configuration option are never recorded. The created_at and updated_at attributes are also ignored.
To ignore more attributes on a model, use addIgnored():
$product->addIgnored(['gross_cost', 'tax_rate']);
Attributes with an encrypted cast (encrypted, encrypted:array, encrypted:collection, encrypted:json, encrypted:object, AsEncryptedArrayObject and AsEncryptedCollection) are never recorded. If they were, the history table would contain the decrypted value.
Development
Testing
Run tests:
composer test