diff --git a/README.md b/README.md
index 9ffc492..57f119d 100644
--- a/README.md
+++ b/README.md
@@ -89,6 +89,28 @@ class User extends Model {
}
```
+
+
+### Hidden fields
+
+There are times you might want to include hidden fields in the version data. You might have hidden the fields with the `visible` or `hidden` properties in your model.
+
+You can have those fields that are typically hidden in the rest of your project saved in the version data by adding them to the `versionedHiddenFields` property of the versionable model.
+
+```php
+class User {
+
+ use VersionableTrait;
+
+ // Typically hidden fields
+ protected $hidden = ['email', 'password'];
+
+ // Save these hidden fields
+ protected $versionedHiddenFields = ['email', 'password'];
+
+}
+```
+
### Maximum number of stored versions
diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php
index 2c44970..9be633a 100644
--- a/src/Mpociot/Versionable/VersionableTrait.php
+++ b/src/Mpociot/Versionable/VersionableTrait.php
@@ -173,7 +173,11 @@ protected function versionablePostSave()
$version->versionable_id = $this->getKey();
$version->versionable_type = method_exists($this, 'getMorphClass') ? $this->getMorphClass() : get_class($this);
$version->user_id = $this->getAuthUserId();
+
+ $versionedHiddenFields = $this->versionedHiddenFields ?? [];
+ $this->makeVisible($versionedHiddenFields);
$version->model_data = serialize($this->attributesToArray());
+ $this->makeHidden($versionedHiddenFields);
if (!empty( $this->reason )) {
$version->reason = $this->reason;
diff --git a/tests/VersionableTest.php b/tests/VersionableTest.php
index c46baeb..286f26f 100644
--- a/tests/VersionableTest.php
+++ b/tests/VersionableTest.php
@@ -488,6 +488,31 @@ public function testKeepMaxVersionCount()
$this->assertEquals( $name_v3, $model->name );
}
+
+ public function testAllowHiddenFields() {
+ $user = new TestHiddenFieldsUser();
+ $user->name = "Marcel";
+ $user->email = "m.pociot@test.php";
+ $user->password = "12345";
+ $user->save();
+ sleep(1);
+
+ $user->name = "John";
+ $user->email = "j.barlow@test.php";
+ $user->password = "6789";
+ $user->save();
+ sleep(1);
+
+ $diff = $user->previousVersion()->diff();
+
+ $this->assertArrayHasKey('email', $diff);
+ $this->assertArrayHasKey('password', $diff);
+ $this->assertEquals( 'John', $diff['name'] );
+ $this->assertEquals( 'j.barlow@test.php', $diff['email'] );
+ $this->assertEquals( '6789', $diff['password'] );
+
+ $this->assertArrayNotHasKey('password', $user->toArray());
+ }
}
@@ -545,3 +570,12 @@ class ModelWithJsonField extends Model
protected $casts = ['json_field' => 'array'];
}
+class TestHiddenFieldsUser extends \Illuminate\Foundation\Auth\User {
+ use \Mpociot\Versionable\VersionableTrait;
+
+ protected $table = "users";
+
+ protected $hidden = ['email', 'password'];
+
+ protected $versionedHiddenFields = ['email', 'password'];
+}