Skip to content

Exception : Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::getOwnerKey() #3385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
waleedfaridworkland opened this issue May 21, 2025 · 0 comments

Comments

@waleedfaridworkland
Copy link

  • Laravel-mongodb Version: 5.4.1
  • PHP Version: 8.2
  • Database Driver & Version: 2.0.0

Description:

We have a micro services architecture and we work with multiple databases within MySQL.

In our laravel-lumen application version 10, when we specify different mysql models using different database names through the connection property, while using HybridRelations, we get the error

Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getOwnerKeyName()

  1. Model A uses hybrid relations and its table resides in mysql database 1
  • It has a many to many relationship with Model B.
  • It also has a one to one relationship with Model C.
  • It has property protected $connection = database_1
  1. Model B does not use hybrid relations and its table resides in mysql database 2
  • It has a many to many relationship with Model A.
  • It has property protected $connection = database_2
  1. Model C has a collection that resides in MongoDB.

Expected behaviour

When querying Model A like so :

$candidateUserIds = A::whereHas('B', function ($query) use ($b_ids) {
$query->whereIn('b_table.id', $b_ids);
})->get();

It should return results, respecting the condition on its relationship.

Actual behaviour

Get the error :
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::getOwnerKeyName()

This is because of this function here :
mongodb > laravel-mongodb > src > Helpers > trait QueriesRelationships

public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null)

In particular line 56 

    if (Model::isDocumentModel($this->getModel()) || $this->isAcrossConnections($relation)) {
        return $this->addHybridHas($relation, $operator, $count, $boolean, $callback);
    }

The following function returns true.

protected function isAcrossConnections(Relation $relation)
{
    return $relation->getParent()->getConnectionName() !== $relation->getRelated()->getConnectionName();
}

In my case both model A and model B are in mysql, but because they have different connection names, they are considered hybrid relationships and laravel thinks that Model B is in MongoDB.

The temporary fix was to remove the connection property from Model B and specify the database name in the property
protected $table = 'database_2.b_table';

Would it be possible to check the driver property of the connections instead ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants