@@ -14,7 +14,7 @@ trait VersionableTrait
1414 /**
1515 * Retrieve, if exists, the property that define that Version model.
1616 * If no property defined, use the default Version model.
17- *
17+ *
1818 * Trait cannot share properties whth their class !
1919 * http://php.net/manual/en/language.oop5.traits.php
2020 * @return unknown|string
@@ -28,6 +28,16 @@ protected function getVersionClass()
2828 return config ('versionable.version_model ' , Version::class);
2929 }
3030
31+ /**
32+ * Get the encoding, the default is serialize.
33+ *
34+ * @return string
35+ */
36+ protected function getEncoding ()
37+ {
38+ return config ('versionable.encoding ' , 'serialize ' );
39+ }
40+
3141 /**
3242 * Private variable to detect if this is an update
3343 * or an insert
@@ -173,10 +183,12 @@ protected function versionablePostSave()
173183 $ version ->versionable_id = $ this ->getKey ();
174184 $ version ->versionable_type = method_exists ($ this , 'getMorphClass ' ) ? $ this ->getMorphClass () : get_class ($ this );
175185 $ version ->user_id = $ this ->getAuthUserId ();
176-
186+
177187 $ versionedHiddenFields = $ this ->versionedHiddenFields ?? [];
178188 $ this ->makeVisible ($ versionedHiddenFields );
179- $ version ->model_data = serialize ($ this ->attributesToArray ());
189+ $ version ->model_data = $ this ->getEncoding () === 'json '
190+ ? json_encode ($ this ->attributesToArray ())
191+ : serialize ($ this ->attributesToArray ());
180192 $ this ->makeHidden ($ versionedHiddenFields );
181193
182194 if (!empty ( $ this ->reason )) {
@@ -191,16 +203,16 @@ protected function versionablePostSave()
191203
192204 /**
193205 * Delete old versions of this model when the reach a specific count.
194- *
206+ *
195207 * @return void
196208 */
197209 private function purgeOldVersions ()
198210 {
199211 $ keep = isset ($ this ->keepOldVersions ) ? $ this ->keepOldVersions : 0 ;
200-
212+
201213 if ((int )$ keep > 0 ) {
202214 $ count = $ this ->versions ()->count ();
203-
215+
204216 if ($ count > $ keep ) {
205217 $ this ->getLatestVersions ()
206218 ->take ($ count )
0 commit comments