diff --git a/src/Laravel/Eloquent/State/LinksHandler.php b/src/Laravel/Eloquent/State/LinksHandler.php index 559a506a5e..efcefc41df 100644 --- a/src/Laravel/Eloquent/State/LinksHandler.php +++ b/src/Laravel/Eloquent/State/LinksHandler.php @@ -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 $relation */ + /** @var \Illuminate\Database\Eloquent\Relations\BelongsToMany $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); }