@@ -16,7 +16,10 @@ trait Json
16
16
'#>> ' ];
17
17
18
18
/**
19
- * Holds the map of attributes and the JSON colums they are stored in.
19
+ * Holds the map of attributes and the JSON colums they are stored in. This
20
+ * will take the form of:
21
+ * [ 'json_element_1' => 'original_column',
22
+ * 'json_element_2' => 'original_column' ]
20
23
*
21
24
* @var array
22
25
*/
@@ -247,7 +250,6 @@ protected function mutateAttribute($key, $value)
247
250
return null ;
248
251
}
249
252
250
-
251
253
return parent ::mutateAttribute ($ key , $ value );
252
254
}
253
255
@@ -294,6 +296,34 @@ public function setJsonAttribute($attribute, $key, $value)
294
296
return ;
295
297
}
296
298
299
+ /**
300
+ * Add json attributes to the list of things that have changed (when
301
+ * they've changed)
302
+ *
303
+ * @return Array
304
+ */
305
+ public function getDirty ( $ includeJson = false )
306
+ {
307
+ $ dirty = parent ::getDirty ();
308
+
309
+ if ( !$ includeJson ) {
310
+ return $ dirty ;
311
+ }
312
+
313
+ foreach (array_unique ($ this ->jsonAttributes ) as $ attribute ) {
314
+ $ originals [$ attribute ] = json_decode (array_get ($ this ->original ,$ attribute ,'null ' ), true );
315
+ }
316
+
317
+ foreach ($ this ->jsonAttributes as $ jsonAttribute => $ jsonColumn ) {
318
+ if ($ this ->$ jsonAttribute !== null &&
319
+ $ this ->$ jsonAttribute !== array_get ($ originals [$ jsonColumn ], $ jsonAttribute )) {
320
+ $ dirty [$ jsonAttribute ] = json_encode ($ this ->$ jsonAttribute );
321
+ }
322
+ }
323
+
324
+ return $ dirty ;
325
+ }
326
+
297
327
/**
298
328
* Allows you to specify if the actual JSON column housing the attributes
299
329
* should be shown on toArray() and toJson() calls. Set this value in the
0 commit comments