Skip to content

Unable to use hasNested with morphTo relationship #56490

@nicDamours

Description

@nicDamours

Laravel Version

12.21.0

PHP Version

8.3.6

Database Driver & Version

MariaDB 10.6

Description

I'm having the following error when attempting to use the whereHas function to query existence of a nested morphTo relationship.

Error: Call to a member function getRelationExistenceQuery() on null
full stacktrace
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:60
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:105
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:276
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1572
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:68
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:163
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:281
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:325
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:365
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:271
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:325
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:269
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:50
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:108
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:43
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:163
  /var/www/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2518
  /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2534
  /var/www/tests/Unit/NameableSuffixTest.php:29

The source of the issues seams to be the $relations variable, in the \Illuminate\Database\Eloquent\Concerns\QueriesRelationships::hasNested function.

This variable is empty when passing in the $closure with a second morph type. Everything is fine for the first morph type, but since the hasNested function is only called once, when we get into the closure for the subsequent morph type, the $relations variable is not "reset" and stays empty.

I found a way to fix the issues, and I will create a PR, but I'm not sure if the proposed solution is the best one.

Steps To Reproduce

I've created a repository to reproduce the issue.

https://github.com/nicDamours/LaravelHasNestedMorphToBug

This repository contains a docker-compose.yml file to help reproduce the issue.

If using docker:

  • You have to start the docker using the command docker compose up -d
  • you have to run the command in the docker using docker exec -it {container-name} /bin/bash.

Here are the steps to reproduce:

  1. Clone the repository
  2. copy the .env.example file to .env
  3. Create the app key using the command php artisan key:generate
  4. install the composer dependencies
  5. run the php artisan test command
  6. Notice the error.

The test NameableSuffixTest::test_it_cannot_have_morphTo_and_hasNested_combined is used to reproduce the error and as you can see, the issue comes from the following line:

$results = Nameable::whereHas('entity.suffixes')->get();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions