Skip to content

Commit

Permalink
feat: add pivot data
Browse files Browse the repository at this point in the history
  • Loading branch information
saade committed Mar 5, 2024
1 parent 7841cfb commit 4fcc9c4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Filament\Support\Facades\FilamentIcon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class SelectTree extends Field implements HasAffixActions
Expand Down Expand Up @@ -103,16 +104,19 @@ protected function setUp(): void
// Check if the component's relationship is a BelongsToMany relationship.
if ($component->getRelationship() instanceof BelongsToMany) {
// Wrap the state in a collection and convert it to an array if it's not set.
$state = Collection::wrap($state ?? []);
$state = Arr::wrap($state ?? []);

if($pivotData = $component->getPivotData()) {
$component->getRelationship()->syncWithPivotValues($state->toArray(), $pivotData);

$pivotData = $component->getPivotData();

// Sync the relationship with the provided state (IDs).
if ($pivotData === []) {
$component->getRelationship()->sync($state ?? []);

return;
}

// Sync the relationship with the provided state (IDs).
$component->getRelationship()->sync($state->toArray());
// Sync the relationship with the provided state (IDs) plus pivot data.
$component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData);
}
});

Expand Down

0 comments on commit 4fcc9c4

Please sign in to comment.