Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Mar 20, 2024
1 parent eeb5325 commit 06ec57c
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/Components/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,51 @@ private function getAttachedKeysFromRelation(string $relationName): ?array
{
// Eager load the relation if not, to avoid duplicate queries
if (!$this->model->relationLoaded($relationName)) {
$this->model->load($relationName);
return $this->queryAttachedKeysFromRelation($relationName);
}

$loadedRelation = $this->model->{$relationName};
$relationInstance = $this->model->{$relationName}();

// Get relationship method to check type
$relationValue = $this->model->{$relationName};

if ($relationInstance instanceof BelongsToMany) {
return $relationValue->pluck($relationInstance->getRelatedKeyName())
->map(fn ($key) => (string) $key)
->all();
}

if ($relationInstance instanceof MorphMany) {
return $relationValue->pluck($relationInstance->getLocalKeyName())
->map(fn ($key) => (string) $key)
->all();
}

return [];
}

/**
* Queries the attached keys from the given relationship.
*/
private function queryAttachedKeysFromRelation(string $relationName): ?array
{
$relation = $this->model->{$relationName}();

if ($relation instanceof BelongsToMany) {
return $loadedRelation->pluck($relation->getRelatedKeyName())
$relatedKeyName = $relation->getRelatedKeyName();

return $relation->getBaseQuery()
->get($relation->getRelated()->qualifyColumn($relatedKeyName))
->pluck($relatedKeyName)
->map(fn ($key) => (string) $key)
->all();
}

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

return $relation->getBaseQuery()
->get($relation->getQuery()->qualifyColumn($parentKeyName))
->pluck($parentKeyName)
->map(fn ($key) => (string) $key)
->all();
}
Expand Down

0 comments on commit 06ec57c

Please sign in to comment.