10
10
use Filament \Forms \Components \Concerns \CanBeSearchable ;
11
11
use Filament \Forms \Components \Concerns \HasActions ;
12
12
use Filament \Forms \Components \Concerns \HasAffixes ;
13
+ use Filament \Forms \Components \Concerns \HasPivotData ;
13
14
use Filament \Forms \Components \Concerns \HasPlaceholder ;
14
15
use Filament \Forms \Components \Contracts \HasAffixActions ;
15
16
use Filament \Forms \Components \Field ;
16
17
use Filament \Forms \Form ;
17
18
use Filament \Support \Facades \FilamentIcon ;
18
19
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
19
20
use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
21
+ use Illuminate \Support \Arr ;
20
22
use Illuminate \Support \Collection ;
21
23
22
24
class SelectTree extends Field implements HasAffixActions
@@ -26,6 +28,7 @@ class SelectTree extends Field implements HasAffixActions
26
28
use HasActions;
27
29
use HasAffixes;
28
30
use HasPlaceholder;
31
+ use HasPivotData;
29
32
30
33
protected string $ view = 'select-tree::select-tree ' ;
31
34
@@ -101,10 +104,19 @@ protected function setUp(): void
101
104
// Check if the component's relationship is a BelongsToMany relationship.
102
105
if ($ component ->getRelationship () instanceof BelongsToMany) {
103
106
// Wrap the state in a collection and convert it to an array if it's not set.
104
- $ state = Collection::wrap ($ state ?? []);
107
+ $ state = Arr::wrap ($ state ?? []);
108
+
109
+ $ pivotData = $ component ->getPivotData ();
105
110
106
111
// Sync the relationship with the provided state (IDs).
107
- $ component ->getRelationship ()->sync ($ state ->toArray ());
112
+ if ($ pivotData === []) {
113
+ $ component ->getRelationship ()->sync ($ state ?? []);
114
+
115
+ return ;
116
+ }
117
+
118
+ // Sync the relationship with the provided state (IDs) plus pivot data.
119
+ $ component ->getRelationship ()->syncWithPivotValues ($ state ?? [], $ pivotData );
108
120
}
109
121
});
110
122
0 commit comments