22
33namespace OwenIt \Auditing ;
44
5+ use Illuminate \Database \Eloquent \Model ;
56use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
67use Illuminate \Database \Eloquent \Relations \MorphMany ;
78use Illuminate \Database \Eloquent \SoftDeletes ;
89use Illuminate \Support \Arr ;
10+ use Illuminate \Support \Collection ;
911use Illuminate \Support \Facades \App ;
1012use Illuminate \Support \Facades \Config ;
1113use Illuminate \Support \Facades \Event ;
@@ -754,7 +756,7 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c
754756
755757 /**
756758 * @param string $relationName
757- * @param \Illuminate\Support\ Collection|\Illuminate\Database\Eloquent\ Model|array $ids
759+ * @param Collection|Model|array $ids
758760 * @param bool $detaching
759761 * @param array $columns
760762 * @param \Closure|null $callback
@@ -787,7 +789,7 @@ public function auditSync(string $relationName, $ids, $detaching = true, $column
787789
788790 /**
789791 * @param string $relationName
790- * @param \Illuminate\Support\ Collection|\Illuminate\Database\Eloquent\ Model|array $ids
792+ * @param Collection|Model|array $ids
791793 * @param array $columns
792794 * @param \Closure|null $callback
793795 * @return array
@@ -800,11 +802,37 @@ public function auditSyncWithoutDetaching(string $relationName, $ids, $columns =
800802 return $ this ->auditSync ($ relationName , $ ids , false , $ columns , $ callback );
801803 }
802804
805+ /**
806+ * @param string $relationName
807+ * @param Collection|Model|array $ids
808+ * @param array $values
809+ * @param bool $detaching
810+ * @param array $columns
811+ * @param \Closure|null $callback
812+ * @return array
813+ */
814+ public function auditSyncWithPivotValues (string $ relationName , $ ids , array $ values , bool $ detaching = true , $ columns = ['* ' ], $ callback = null )
815+ {
816+ $ this ->validateRelationshipMethodExistence ($ relationName , 'syncWithPivotValues ' );
817+
818+ if ($ ids instanceof Model) {
819+ $ ids = $ ids ->getKey ();
820+ } elseif ($ ids instanceof \Illuminate \Database \Eloquent \Collection) {
821+ $ ids = $ ids ->isEmpty () ? [] : $ ids ->pluck ($ ids ->first ()->getKeyName ())->toArray ();
822+ } elseif ($ ids instanceof Collection) {
823+ $ ids = $ ids ->toArray ();
824+ }
825+
826+ return $ this ->auditSync ($ relationName , collect (Arr::wrap ($ ids ))->mapWithKeys (function ($ id ) use ($ values ) {
827+ return [$ id => $ values ];
828+ }), $ detaching , $ columns , $ callback );
829+ }
830+
803831 /**
804832 * @param string $relationName
805833 * @param string $event
806- * @param \Illuminate\Support\ Collection $old
807- * @param \Illuminate\Support\ Collection $new
834+ * @param Collection $old
835+ * @param Collection $new
808836 * @return void
809837 */
810838 private function dispatchRelationAuditEvent ($ relationName , $ event , $ old , $ new )
0 commit comments