Skip to content
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

whereDoesntHave not produce correct query when use with nullableMorphs #54315

Open
mix5003 opened this issue Jan 23, 2025 · 3 comments · May be fixed by #54363
Open

whereDoesntHave not produce correct query when use with nullableMorphs #54315

mix5003 opened this issue Jan 23, 2025 · 3 comments · May be fixed by #54363

Comments

@mix5003
Copy link

mix5003 commented Jan 23, 2025

Laravel Version

11.39.1

PHP Version

8.2.12

Database Driver & Version

sqlite / mysql

Description

When use whereDoesntHave with nullableMorphs relation. it will not count nullable as dosent have.

Steps To Reproduce

prepare project

  1. create new project with laravel 10/11
  2. create MainTable model by php artisan make:model -m MainTable
  3. add $table->nullableMorphs('relate'); to MainTable migration file
  4. add morphTo relation to MainTable Model file public function relate(){ return $this->morphTo(); }
  5. create Relate1model by php artisan make:model -m Relate1
  6. migrate by php artisan migrate

test by this in php artisan tinker

use App\Models\MainTable;
use App\Models\Relate1;

$r1 = new Relate1();
$r1->save();


$m1 = new MainTable();
$m1->save();

$m2 = new MainTable();
$m2->relate()->associate($r1);
$m2->save();

DB::enableQueryLog();
echo MainTable::has('relate')->count(); // 1
echo MainTable::whereDoesntHave('relate')->count(); // 0 expect 1

var_dump(DB::getQueryLog());

it produce this query

select count(*) as aggregate from "main_tables" where (("main_tables"."relate_type" = ? and not exists (select * from "relate1s" where "main_tables"."relate_id" = "relate1s"."id")))

but it should be (add "main_tables"."relate_type" is null or

select count(*) as aggregate from "main_tables" where ("main_tables"."relate_type" is null or ("main_tables"."relate_type" = ? and not exists (select * from "relate1s" where "main_tables"."relate_id" = "relate1s"."id")))
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@mix5003
Copy link
Author

mix5003 commented Jan 24, 2025

like this?
https://github.com/mix5003/laravel-bug-54315

you can clone migrate and run php artisan app:test-query for test result

Copy link

github-actions bot commented Feb 6, 2025

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

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

Successfully merging a pull request may close this issue.

2 participants