@@ -647,6 +647,7 @@ public function transitionTo(Contracts\Audit $audit, bool $old = false): Contrac
647
647
* @param mixed $id
648
648
* @param array $attributes
649
649
* @param bool $touch
650
+ * @param array $columns
650
651
* @return void
651
652
* @throws AuditingException
652
653
*/
@@ -655,23 +656,18 @@ public function auditAttach(string $relationName, $id, array $attributes = [], $
655
656
if (!method_exists ($ this , $ relationName ) || !method_exists ($ this ->{$ relationName }(), 'attach ' )) {
656
657
throw new AuditingException ('Relationship ' . $ relationName . ' was not found or does not support method attach ' );
657
658
}
658
- $ this ->auditEvent = 'attach ' ;
659
- $ this ->isCustomEvent = true ;
660
- $ this ->auditCustomOld = [
661
- $ relationName => $ this ->{$ relationName }()->get ($ columns )->toArray ()
662
- ];
659
+
660
+ $ old = $ this ->{$ relationName }()->get ($ columns );
663
661
$ this ->{$ relationName }()->attach ($ id , $ attributes , $ touch );
664
- $ this ->auditCustomNew = [
665
- $ relationName => $ this ->{$ relationName }()->get ($ columns )->toArray ()
666
- ];
667
- Event::dispatch (AuditCustom::class, [$ this ]);
668
- $ this ->isCustomEvent = false ;
662
+ $ new = $ this ->{$ relationName }()->get ($ columns );
663
+ $ this ->dispatchRelationAuditEvent ($ relationName , 'attach ' , $ old , $ new );
669
664
}
670
665
671
666
/**
672
667
* @param string $relationName
673
668
* @param mixed $ids
674
669
* @param bool $touch
670
+ * @param array $columns
675
671
* @return int
676
672
* @throws AuditingException
677
673
*/
@@ -681,25 +677,19 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c
681
677
throw new AuditingException ('Relationship ' . $ relationName . ' was not found or does not support method detach ' );
682
678
}
683
679
684
- $ this ->auditEvent = 'detach ' ;
685
- $ this ->isCustomEvent = true ;
686
- $ this ->auditCustomOld = [
687
- $ relationName => $ this ->{$ relationName }()->get ($ columns )->toArray ()
688
- ];
680
+ $ old = $ this ->{$ relationName }()->get ($ columns );
689
681
$ results = $ this ->{$ relationName }()->detach ($ ids , $ touch );
690
- $ this ->auditCustomNew = [
691
- $ relationName => $ this ->{$ relationName }()->get ($ columns )->toArray ()
692
- ];
693
- Event::dispatch (AuditCustom::class, [$ this ]);
694
- $ this ->isCustomEvent = false ;
682
+ $ new = $ this ->{$ relationName }()->get ($ columns );
683
+ $ this ->dispatchRelationAuditEvent ($ relationName , 'detach ' , $ old , $ new );
684
+
695
685
return empty ($ results ) ? 0 : $ results ;
696
686
}
697
687
698
688
/**
699
689
* @param $relationName
700
690
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
701
691
* @param bool $detaching
702
- * @param bool $skipUnchanged
692
+ * @param array $columns
703
693
* @return array
704
694
* @throws AuditingException
705
695
*/
@@ -709,34 +699,22 @@ public function auditSync($relationName, $ids, $detaching = true, $columns = ['*
709
699
throw new AuditingException ('Relationship ' . $ relationName . ' was not found or does not support method sync ' );
710
700
}
711
701
712
- $ this ->auditEvent = 'sync ' ;
713
-
714
- $ this ->auditCustomOld = [
715
- $ relationName => $ this ->{$ relationName }()->get ($ columns )->toArray ()
716
- ];
717
-
702
+ $ old = $ this ->{$ relationName }()->get ($ columns );
718
703
$ changes = $ this ->{$ relationName }()->sync ($ ids , $ detaching );
719
-
720
704
if (collect ($ changes )->flatten ()->isEmpty ()) {
721
- $ this ->auditCustomOld = [];
722
- $ this ->auditCustomNew = [];
705
+ $ old = $ new = collect ([]);
723
706
} else {
724
- $ this ->auditCustomNew = [
725
- $ relationName => $ this ->{$ relationName }()->get ($ columns )->toArray ()
726
- ];
707
+ $ new = $ this ->{$ relationName }()->get ($ columns );
727
708
}
728
-
729
- $ this ->isCustomEvent = true ;
730
- Event::dispatch (AuditCustom::class, [$ this ]);
731
- $ this ->isCustomEvent = false ;
709
+ $ this ->dispatchRelationAuditEvent ($ relationName , 'sync ' , $ old , $ new );
732
710
733
711
return $ changes ;
734
712
}
735
713
736
714
/**
737
715
* @param string $relationName
738
716
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
739
- * @param bool $skipUnchanged
717
+ * @param array $columns
740
718
* @return array
741
719
* @throws AuditingException
742
720
*/
@@ -745,6 +723,32 @@ public function auditSyncWithoutDetaching(string $relationName, $ids, $columns =
745
723
if (!method_exists ($ this , $ relationName ) || !method_exists ($ this ->{$ relationName }(), 'syncWithoutDetaching ' )) {
746
724
throw new AuditingException ('Relationship ' . $ relationName . ' was not found or does not support method syncWithoutDetaching ' );
747
725
}
726
+
748
727
return $ this ->auditSync ($ relationName , $ ids , false , $ columns );
749
728
}
729
+
730
+ /**
731
+ * @param string $relationName
732
+ * @param string $event
733
+ * @param \Illuminate\Support\Collection $old
734
+ * @param \Illuminate\Support\Collection $new
735
+ * @return void
736
+ */
737
+ private function dispatchRelationAuditEvent ($ relationName , $ event , $ old , $ new )
738
+ {
739
+ $ this ->auditCustomOld [$ relationName ] = $ old ->toArray ();
740
+ $ this ->auditCustomNew [$ relationName ] = $ new ->toArray ();
741
+
742
+ if (
743
+ empty ($ this ->auditCustomOld [$ relationName ]) &&
744
+ empty ($ this ->auditCustomNew [$ relationName ])
745
+ ) {
746
+ $ this ->auditCustomOld = $ this ->auditCustomNew = [];
747
+ }
748
+
749
+ $ this ->auditEvent = $ event ;
750
+ $ this ->isCustomEvent = true ;
751
+ Event::dispatch (AuditCustom::class, [$ this ]);
752
+ $ this ->isCustomEvent = false ;
753
+ }
750
754
}
0 commit comments