diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index cb377ed..ffca6c6 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -175,7 +175,7 @@ protected function versionablePostSave() $version->versionable_id = $this->getKey(); $version->versionable_type = get_class($this); $version->user_id = $this->getAuthUserId(); - $version->model_data = serialize($this->getAttributes()); + $version->model_data = serialize($this->attributesToArray()); if (!empty( $this->reason )) { $version->reason = $this->reason; diff --git a/tests/VersionableTest.php b/tests/VersionableTest.php index 3296034..7aa580c 100644 --- a/tests/VersionableTest.php +++ b/tests/VersionableTest.php @@ -289,6 +289,15 @@ public function testGetVersionModel() } + public function testGetVersionModelWithJsonField() + { + $model = new ModelWithJsonField(); + $model->json_field = ["foo" => "bar"]; + $model->save(); + + $this->assertEquals(["foo" => "bar"], $model->getVersionModel(1)->json_field); + } + public function testUseReasonAttribute() { // Create 3 versions @@ -547,4 +556,11 @@ class ModelWithDynamicVersion extends Model use VersionableTrait ; protected $versionClass = DynamicVersionModel::class ; } +class ModelWithJsonField extends Model +{ + const TABLENAME = 'table_with_json_field'; + public $table = self::TABLENAME ; + use VersionableTrait ; + protected $casts = ['json_field' => 'array']; +} diff --git a/tests/VersionableTestCase.php b/tests/VersionableTestCase.php index e7278aa..d89b522 100644 --- a/tests/VersionableTestCase.php +++ b/tests/VersionableTestCase.php @@ -65,5 +65,11 @@ public function migrateUsersTable() $table->index('versionable_id'); $table->timestamps(); }); + + $this->app['db']->connection()->getSchemaBuilder()->create(ModelWithJsonField::TABLENAME, function ($table) { + $table->increments('id'); + $table->json('json_field'); + $table->timestamps(); + }); } } \ No newline at end of file