Skip to content

Commit d3234ca

Browse files
Merge pull request #89 from saade/fix/relationship-dehydration
revert: relationship dehydration
2 parents 68ddc29 + 0248da5 commit d3234ca

File tree

1 file changed

+6
-27
lines changed

1 file changed

+6
-27
lines changed

src/SelectTree.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,38 +93,19 @@ protected function setUp(): void
9393

9494
// Set the component's state with the extracted IDs.
9595
$component->state($state);
96-
97-
return;
98-
}
99-
100-
/** @var BelongsTo $relationship */
101-
if (! $relatedModel = $relationship->getResults()) {
102-
return;
10396
}
104-
105-
$component->state(
106-
$relatedModel->getAttribute(
107-
$relationship->getOwnerKeyName(),
108-
),
109-
);
11097
});
11198

11299
// Save relationships using a callback function.
113100
$this->saveRelationshipsUsing(static function (self $component, $state) {
114-
$relationship = $component->getRelationship();
101+
// Check if the component's relationship is a BelongsToMany relationship.
102+
if ($component->getRelationship() instanceof BelongsToMany) {
103+
// Wrap the state in a collection and convert it to an array if it's not set.
104+
$state = Collection::wrap($state ?? []);
115105

116-
// Check if the component's relationship is a BelongsTo relationship.
117-
if ($relationship instanceof BelongsTo) {
118-
$relationship->associate($state)->save();
119-
120-
return;
106+
// Sync the relationship with the provided state (IDs).
107+
$component->getRelationship()->sync($state->toArray());
121108
}
122-
123-
// Wrap the state in a collection and convert it to an array if it's not set.
124-
$state = Collection::wrap($state ?? []);
125-
126-
// Sync the relationship with the provided state (IDs).
127-
$component->getRelationship()->sync($state->toArray());
128109
});
129110

130111
$this->createOptionUsing(static function (SelectTree $component, array $data, Form $form) {
@@ -140,8 +121,6 @@ protected function setUp(): void
140121
$this->suffixActions([
141122
static fn (SelectTree $component): ?Action => $component->getCreateOptionAction(),
142123
]);
143-
144-
$this->dehydrated(false);
145124
}
146125

147126
private function buildTree(): Collection

0 commit comments

Comments
 (0)