diff --git a/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php b/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php index 99798ea4f..14d72f622 100644 --- a/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php +++ b/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php @@ -153,16 +153,18 @@ protected function getConstrainedRelatedIds($relations, $operator, $count) */ protected function getRelatedConstraintKey($relation) { + $prefix = $this->model instanceof Model ? '' : $this->model->getTable().'.'; + if ($relation instanceof HasOneOrMany) { - return $this->model->getKeyName(); + return $prefix.$this->model->getKeyName(); } if ($relation instanceof BelongsTo) { - return $relation->getForeignKeyName(); + return $prefix.$relation->getForeignKeyName(); } if ($relation instanceof BelongsToMany && !$this->isAcrossConnections($relation)) { - return $this->model->getKeyName(); + return $prefix.$this->model->getKeyName(); } throw new Exception(class_basename($relation) . ' is not supported for hybrid query constraints.');