ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      The hybrid `hasOneThrough()` and `hasManyThrough()` relations added in 5.12 (#3627) work when the far parent is a SQL model, but the `has()` family always throws.

      Reproduction

      A SQL model uses `HybridRelations` and reaches a MongoDB chain:

      $user->filmsThroughDirectors();                            // works
      SqlUser::query()->with('filmsThroughDirectors')->get();    // works
      SqlUser::query()->has('filmsThroughDirectors')->get();     // throws
      SqlUser::query()->whereHas('filmsThroughDirectors')->get();// throws
      SqlUser::query()->withCount('filmsThroughDirectors')->get(); // throws
      

      The exception is the one `ResolvesThroughKeys::getRelationExistenceQuery()` raises on purpose:

      LogicException: Through relations cannot be used in existence queries: Eloquent builds the constraint from a join, which MongoDB does not have. Use has() or whereHas() instead.
      

      The message suggests `has()` or `whereHas()`, but those are the calls that fail. `tests/HybridRelationsTest.php` covers only the relation itself (instance and lazy load), and `tests/ThroughRelationsTest.php` exercises `has()` and `withCount()` on document parents only.

      Cause

      `QueriesRelationships::has()` (`src/Helpers/QueriesRelationships.php:54`) routes the relation to `addHybridHas()` when the parent is a document model or when `isAcrossConnections()` is true.

      `isAcrossConnections()` (`src/Helpers/QueriesRelationships.php:87-90`) compares the connection names of `getParent()` and `getRelated()`. For a through relation, Eloquent passes the through model as the relation parent: `HasOneOrManyThrough::_construct()` ends with `parent::_construct($query, $throughParent)` (`vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php:82`). `getParent()` is therefore the through model, not the far parent. In the reproduction, the through model (Director) and the related model (Film) are both on `mongodb`, so the comparison is false.

      The Eloquent branch then calls `getRelationExistenceQuery()`, which `MongoDB\Laravel\Relations\ResolvesThroughKeys::getRelationExistenceQuery()` (`src/Relations/ResolvesThroughKeys.php:85-88`) throws by design, because a through relation has no join.

      `withCount()` and `withExists()` on a SQL model do not use the package helper at all. They reach the same `getRelationExistenceQuery()` through `Illuminate\Database\Eloquent\Concerns\QueriesRelationships::withAggregate()`, so they throw the same exception.

      Suggested direction

      Treat `ThroughRelation` as a hybrid relation in `QueriesRelationships::has()`, so `addHybridHas()` is taken whenever the relation is hybrid, not only when the far parent is a document model or `isAcrossConnections()` happens to be true. `assertHybridRelationSupported()` and `getRelatedConstraintKey()` already accept `ThroughRelation` (`src/Helpers/QueriesRelationships.php:139-150` and `:233-249`), and `whereHas()` follows the same path.

      `withCount()` and `withExists()` need a separate decision, because Eloquent's `withAggregate()` builds its subquery from `getRelationExistenceQuery()`. Either extend the SQL builder with the same matched-aggregate logic the document builder uses (`src/Helpers/QueriesRelationshipAggregates.php`), or reject the call with a message that names a working alternative.

      Add tests in `tests/HybridRelationsTest.php` for `has()`, `whereHas()`, `withCount()` and `withExists()` on `SqlUser::filmsThroughDirectors()`.

              Assignee:
              Pauline Vos
              Reporter:
              Jérôme Tamarelle
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated: