Skip to content

Commit

Permalink
Fix select duplicate queries (#466)
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Baljet <[email protected]>
  • Loading branch information
KinArnaud and pascalbaljet authored Mar 20, 2024
1 parent 5896933 commit 6316e96
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/Components/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,24 +373,18 @@ private function guardedData(): ?object
*/
private function getAttachedKeysFromRelation(string $relationName): ?array
{
$relation = $this->model->{$relationName}();
$relationInstance = $this->model->{$relationName}();

if ($relation instanceof BelongsToMany) {
$relatedKeyName = $relation->getRelatedKeyName();
$relationValue = $this->model->{$relationName};

return $relation->getBaseQuery()
->get($relation->getRelated()->qualifyColumn($relatedKeyName))
->pluck($relatedKeyName)
if ($relationInstance instanceof BelongsToMany) {
return $relationValue->pluck($relationInstance->getRelatedKeyName())
->map(fn ($key) => (string) $key)
->all();
}

if ($relation instanceof MorphMany) {
$parentKeyName = $relation->getLocalKeyName();

return $relation->getBaseQuery()
->get($relation->getQuery()->qualifyColumn($parentKeyName))
->pluck($parentKeyName)
if ($relationInstance instanceof MorphMany) {
return $relationValue->pluck($relationInstance->getLocalKeyName())
->map(fn ($key) => (string) $key)
->all();
}
Expand Down

0 comments on commit 6316e96

Please sign in to comment.