Skip to content

Exception: BelongsToMany Is Not supported for hybrid query constraints! #1293

Closed
@benargo

Description

@benargo

My application is throwing the following exception on my many-to-many (BelongsToMany) relationships, even though I'm not using hybrid relations.

This appears to have cropped up ever since #1195 was merged. I have been running using 'dev-master' for testing. Rolling back to the latest stable release (v3.2.2) fixes the issue (although the issue fixed by #1158 still hasn't been tagged into the stable release).

Here's the exception and stack trace:

[2017-09-07 12:56:36] local.ERROR: Exception: BelongsToMany Is Not supported for hybrid query constraints! in /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php:115
Stack trace:
#0 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php(96): Jenssegers\Mongodb\Eloquent\Builder->getRelatedConstraintKey(Object(Jenssegers\Mongodb\Relations\BelongsToMany))
#1 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php(33): Jenssegers\Mongodb\Eloquent\Builder->addHybridHas(Object(Jenssegers\Mongodb\Relations\BelongsToMany), '>=', 1, 'and', Object(Closure))
#2 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php(121): Jenssegers\Mongodb\Eloquent\Builder->has(Object(Jenssegers\Mongodb\Relations\BelongsToMany), '>=', 1, 'and', Object(Closure))
#3 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1329): Illuminate\Database\Eloquent\Builder->whereHas('venues', Object(Closure))
#4 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Eloquent/Model.php(420): Illuminate\Database\Eloquent\Model->__call('whereHas', Array)
#5 /Users/b-argo/Dropbox/Projects/majic-lamp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1341): Jenssegers\Mongodb\Eloquent\Model->__call('whereHas', Array)
#6 /Users/b-argo/Dropbox/Projects/majic-lamp/app/Http/Controllers/Directory/CategoriesController.php(45): Illuminate\Database\Eloquent\Model::__callStatic('whereHas', Array)
#7 [internal function]: Genii\Http\Controllers\Directory\CategoriesController->getDrinkCategories(Object(Illuminate\Http\Request))

This is the code block which is causing the exception:

$categories = Drink::whereHas('venues', function ($query) use ($location) {
    $query->where('address.geocode', 'near', [
        '$geometry' => ['type' => 'Point', 'coordinates' => [
            $location['longitude'],
            $location['latitude'],
        ]],
        '$maxDistance' => config('location.radius')
    ]);
})->get(['name']);

Having done a bit more digging, it appears to be passing the ($this->getModel() instanceof Model) conditional, found on line 30 of Helpers\QueriesRelations.php and therefore treating it as a hybrid relationship. I have been able to temporarily skirt round the issue by adding this to Helpers\QueriesRelations@getRelatedConstraintKey.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions