Skip to content

Commit 4fcc9c4

Browse files
committed
feat: add pivot data
1 parent 7841cfb commit 4fcc9c4

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/SelectTree.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Filament\Support\Facades\FilamentIcon;
1919
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2020
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
21+
use Illuminate\Support\Arr;
2122
use Illuminate\Support\Collection;
2223

2324
class 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

0 commit comments

Comments
 (0)