Skip to content

Incorrect query is generated when loading morphTo relation on a model with overridden newQuery() method #55652

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
n-hor opened this issue May 5, 2025 · 5 comments

Comments

@n-hor
Copy link

n-hor commented May 5, 2025

Laravel Version

11

PHP Version

8.4

Database Driver & Version

No response

Description

When a model has an overridden newQuery() method, loading a morphTo relation via ->load() results in an incorrect query being generated.

Steps To Reproduce

class User extends Model
{
    protected $fillable = ['name'];
    
}

class Image extends Model
{
    protected $fillable = ['url'];
    
    public function newQuery()
    {
       return parent::newQuery()->whereNotNull('url');
    }
    
    public function owner()
    {
        return $this->morphTo('owner');
    }
}

$image = Image::find(1);
//An invalid query is generated for the related model:
//select * from "users" where "url" is not null and "users"."id" in (1)

$image->load('owner');

Link to code snippet.

@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!

@macropay-solutions
Copy link

macropay-solutions commented May 6, 2025

@n-hor Can you confirm the issue happens also with a global scope instead of your in line condition?

    public function newQuery()
    {
        return $this->registerGlobalScopes($this->newQueryWithoutScopes());
    }

@n-hor
Copy link
Author

n-hor commented May 6, 2025

There is no issue when registering this condition as a global scope.

It seems the issue is caused in Illuminate/Database/Eloquent/Relations/MorphTo.php:149
where the call to ->mergeConstraintsFrom($this->getQuery()) merges where condition from the images table into the users table (as in the example above).

@macropay-solutions
Copy link

We think that newQuery() was not meant to be used in the way you use it but, maybe others could step in and confirm.

Your condition is in fact a global scope.

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

3 participants