Skip to content

Commit

Permalink
Merge pull request #12800 from filamentphp/fix/pivot-table-sync-not-r…
Browse files Browse the repository at this point in the history
…especting-query-scope

fix: Pivot table sync not respecting query scopes
  • Loading branch information
danharrin committed May 15, 2024
2 parents b36f863 + c5e07f4 commit a7a7723
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
45 changes: 37 additions & 8 deletions packages/forms/src/Components/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,34 +168,63 @@ public function relationship(string | Closure | null $name = null, string | Clos
->toArray();
});

$this->loadStateFromRelationshipsUsing(static function (CheckboxList $component, ?array $state): void {
$this->loadStateFromRelationshipsUsing(static function (CheckboxList $component, ?array $state) use ($modifyQueryUsing): void {
$relationship = $component->getRelationship();

/** @var Collection $relatedModels */
$relatedModels = $relationship->getResults();
if ($modifyQueryUsing) {
$component->evaluate($modifyQueryUsing, [
'query' => $relationship->getQuery(),
]);
}

/** @var Collection $relatedRecords */
$relatedRecords = $relationship->getResults();

$component->state(
// Cast the related keys to a string, otherwise Livewire does not
// know how to handle deselection.
//
// https://github.com/filamentphp/filament/issues/1111
$relatedModels
$relatedRecords
->pluck($relationship->getRelatedKeyName())
->map(static fn ($key): string => strval($key))
->toArray(),
->all(),
);
});

$this->saveRelationshipsUsing(static function (CheckboxList $component, ?array $state) {
$this->saveRelationshipsUsing(static function (CheckboxList $component, ?array $state) use ($modifyQueryUsing) {
$relationship = $component->getRelationship();

if ($modifyQueryUsing) {
$component->evaluate($modifyQueryUsing, [
'query' => $relationship->getQuery(),
]);
}

/** @var Collection $relatedRecords */
$relatedRecords = $relationship->getResults();

$recordsToDetach = array_diff(
$relatedRecords
->pluck($relationship->getRelatedKeyName())
->map(static fn ($key): string => strval($key))
->all(),
$state ?? [],
);

if (count($recordsToDetach) > 0) {
$relationship->detach($recordsToDetach);
}

$pivotData = $component->getPivotData();

if ($pivotData === []) {
$component->getRelationship()->sync($state ?? []);
$relationship->sync($state ?? [], detaching: false);

return;
}

$component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData);
$relationship->syncWithPivotValues($state ?? [], $pivotData, detaching: false);
});

$this->dehydrated(false);
Expand Down
43 changes: 35 additions & 8 deletions packages/forms/src/Components/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public function relationship(string | Closure | null $name = null, string | Clos
->toArray();
});

$this->loadStateFromRelationshipsUsing(static function (Select $component, $state): void {
$this->loadStateFromRelationshipsUsing(static function (Select $component, $state) use ($modifyQueryUsing): void {
if (filled($state)) {
return;
}
Expand All @@ -813,18 +813,24 @@ public function relationship(string | Closure | null $name = null, string | Clos
($relationship instanceof BelongsToMany) ||
($relationship instanceof HasManyThrough)
) {
/** @var Collection $relatedModels */
$relatedModels = $relationship->getResults();
if ($modifyQueryUsing) {
$component->evaluate($modifyQueryUsing, [
'query' => $relationship->getQuery(),
]);
}

/** @var Collection $relatedRecords */
$relatedRecords = $relationship->getResults();

$component->state(
// Cast the related keys to a string, otherwise JavaScript does not
// know how to handle deselection.
//
// https://github.com/filamentphp/filament/issues/1111
$relatedModels
$relatedRecords
->pluck(($relationship instanceof BelongsToMany) ? $relationship->getRelatedKeyName() : $relationship->getRelated()->getKeyName())
->map(static fn ($key): string => strval($key))
->toArray(),
->all(),
);

return;
Expand Down Expand Up @@ -948,7 +954,7 @@ static function (Select $component): bool {
},
);

$this->saveRelationshipsUsing(static function (Select $component, Model $record, $state) {
$this->saveRelationshipsUsing(static function (Select $component, Model $record, $state) use ($modifyQueryUsing) {
$relationship = $component->getRelationship();

if (
Expand Down Expand Up @@ -976,15 +982,36 @@ static function (Select $component): bool {
return;
}

if ($modifyQueryUsing) {
$component->evaluate($modifyQueryUsing, [
'query' => $relationship->getQuery(),
]);
}

/** @var Collection $relatedRecords */
$relatedRecords = $relationship->getResults();

$recordsToDetach = array_diff(
$relatedRecords
->pluck($relationship->getRelatedKeyName())
->map(static fn ($key): string => strval($key))
->all(),
$state ?? [],
);

if (count($recordsToDetach) > 0) {
$relationship->detach($recordsToDetach);
}

$pivotData = $component->getPivotData();

if ($pivotData === []) {
$relationship->sync($state ?? []);
$relationship->sync($state ?? [], detaching: false);

return;
}

$relationship->syncWithPivotValues($state ?? [], $pivotData);
$relationship->syncWithPivotValues($state ?? [], $pivotData, detaching: false);
});

$this->createOptionUsing(static function (Select $component, array $data, Form $form) {
Expand Down

0 comments on commit a7a7723

Please sign in to comment.