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 18
18
use Filament \Support \Facades \FilamentIcon ;
19
19
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
20
20
use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
21
+ use Illuminate \Support \Arr ;
21
22
use Illuminate \Support \Collection ;
22
23
23
24
class SelectTree extends Field implements HasAffixActions
@@ -103,16 +104,19 @@ protected function setUp(): void
103
104
// Check if the component's relationship is a BelongsToMany relationship.
104
105
if ($ component ->getRelationship () instanceof BelongsToMany) {
105
106
// 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 ?? []);
107
108
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
+
111
115
return ;
112
116
}
113
117
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 );
116
120
}
117
121
});
118
122
You can’t perform that action at this time.
0 commit comments