Skip to content

Commit

Permalink
feat(laravel): belongs to many relations (api-platform#6818)
Browse files Browse the repository at this point in the history
  • Loading branch information
amermchaudhary authored Nov 29, 2024
1 parent eb53218 commit 2d59c63
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Laravel/Eloquent/State/LinksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ private function buildQuery(Builder $builder, Link $link, mixed $identifier): Bu
if ($from = $link->getFromProperty()) {
$relation = $this->application->make($link->getFromClass());

if (!method_exists($relation->{$from}(), 'getQualifiedForeignKeyName') && method_exists($relation->{$from}(), 'getQualifiedForeignPivotKeyName')) {
/** @var \Illuminate\Database\Eloquent\Relations\BelongsToMany<Model, Model> $relation */
/** @var \Illuminate\Database\Eloquent\Relations\BelongsToMany<Model, Model> $relation_query */
$relation_query = $relation->{$from}();

return $builder->getModel()->join(
$relation_query->getTable(), $relation->{$from}()->getQualifiedForeignPivotKeyName(), $builder->getModel()->getQualifiedKeyName())
->where($relation->{$from}()->getQualifiedForeignPivotKeyName(),
$identifier);
}

return $builder->getModel()->where($relation->{$from}()->getQualifiedForeignKeyName(), $identifier);
}

Expand Down

0 comments on commit 2d59c63

Please sign in to comment.