@@ -199,8 +199,8 @@ public function toAudit()
199
199
// Auditable data
200
200
return [
201
201
'id ' => (string ) Uuid::uuid4 (),
202
- 'old ' => $ this ->oldData ,
203
- 'new ' => $ this ->newData ,
202
+ 'old ' => $ this ->cleanHiddenAuditAttributes ( $ this -> oldData ) ,
203
+ 'new ' => $ this ->cleanHiddenAuditAttributes ( $ this -> newData ) ,
204
204
'type ' => $ this ->typeAuditing ,
205
205
'auditable_id ' => $ this ->getKey (),
206
206
'auditable_type ' => $ this ->getMorphClass (),
@@ -342,4 +342,53 @@ public function isTypeAuditable($key)
342
342
343
343
return false ;
344
344
}
345
+
346
+ /**
347
+ * Whether to clean the attributes which are hidden or not visible.
348
+ *
349
+ * @return bool
350
+ */
351
+ public function isAuditRespectsHidden ()
352
+ {
353
+ return isset ($ this ->auditRespectsHidden ) && $ this ->auditRespectsHidden ;
354
+ }
355
+
356
+ /**
357
+ * Remove the value of attributes which are hidden or not visible on the model.
358
+ *
359
+ * @param $attributes
360
+ *
361
+ * @return array
362
+ */
363
+ public function cleanHiddenAuditAttributes (array $ attributes )
364
+ {
365
+ if ($ this ->isAuditRespectsHidden ()) {
366
+
367
+ // Get hidden and visible attributes from the model
368
+ $ hidden = $ this ->getHidden ();
369
+ $ visible = $ this ->getVisible ();
370
+
371
+ // If visible is set, set to null any attributes which are not in visible
372
+ if (count ($ visible ) > 0 ) {
373
+ foreach ($ attributes as $ key => &$ value ) {
374
+ if (!in_array ($ key , $ visible )) {
375
+ $ value = null ;
376
+ }
377
+ }
378
+ }
379
+
380
+ unset($ value );
381
+
382
+ // If hidden is set, set to null any attributes which are in hidden
383
+ if (count ($ hidden ) > 0 ) {
384
+ foreach ($ hidden as $ key ) {
385
+ if (array_key_exists ($ key , $ attributes )) {
386
+ $ attributes [$ key ] = null ;
387
+ }
388
+ }
389
+ }
390
+ }
391
+
392
+ return $ attributes ;
393
+ }
345
394
}
0 commit comments