File tree Expand file tree Collapse file tree 1 file changed +10
-6
lines changed Expand file tree Collapse file tree 1 file changed +10
-6
lines changed Original file line number Diff line number Diff line change 1818use Filament \Support \Facades \FilamentIcon ;
1919use Illuminate \Database \Eloquent \Relations \BelongsTo ;
2020use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
21+ use Illuminate \Support \Arr ;
2122use Illuminate \Support \Collection ;
2223
2324class SelectTree extends Field implements HasAffixActions
@@ -103,16 +104,19 @@ protected function setUp(): void
103104 // Check if the component's relationship is a BelongsToMany relationship.
104105 if ($ component ->getRelationship () instanceof BelongsToMany) {
105106 // Wrap the state in a collection and convert it to an array if it's not set.
106- $ state = Collection ::wrap ($ state ?? []);
107+ $ state = Arr ::wrap ($ state ?? []);
107108
108- if ($ pivotData = $ component ->getPivotData ()) {
109- $ component ->getRelationship ()->syncWithPivotValues ($ state ->toArray (), $ pivotData );
110-
109+ $ pivotData = $ component ->getPivotData ();
110+
111+ // Sync the relationship with the provided state (IDs).
112+ if ($ pivotData === []) {
113+ $ component ->getRelationship ()->sync ($ state ?? []);
114+
111115 return ;
112116 }
113117
114- // Sync the relationship with the provided state (IDs).
115- $ component ->getRelationship ()->sync ($ state-> toArray () );
118+ // Sync the relationship with the provided state (IDs) plus pivot data .
119+ $ component ->getRelationship ()->syncWithPivotValues ($ state ?? [], $ pivotData );
116120 }
117121 });
118122
You can’t perform that action at this time.
0 commit comments