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

MorphMany used with $withCount property insert an extra binding into the queries using a nested where #23957

Closed
Tucker-Eric opened this issue Apr 20, 2018 · 1 comment

Comments

@Tucker-Eric
Copy link
Contributor

  • Laravel Version: 5.6.16
  • PHP Version: 7.2
  • Database Driver & Version: mysql

Description:

An extra binding is inserted into a query of a model that references a morphMany relationship in it's $withCount property when using a nested where() statement.

Steps To Reproduce:

class ParentModel extends Model 
{
    protected $table = 'parent';

    protected $withCount = ['children'];

    public function children()
    {
        return $this->morphMany(ChildModel::class, 'morphable');
    }
}

class ChildModel extends Model
{
    protected $table = 'child';
}

$parent = new ParentModel;

$query = $parent->newQuery()->where(function($q) {
    return $q->where(1,1);
});

$bindings = $query->getBindings();

Expected bindings:

['ParentModel', 1];

Expected query:

select `parent`.*, (select count(*) from `child` where `parent`.`id` = `child`.`morphable_id` and `child`.`morphable_type` = 'ParentModel') as `children_count` from `parent` where (1 = 1)

Actual Bindings

['ParentModel','ParentModel', 1];

Actual query:

select `parent`.*, (select count(*) from `child` where `parent`.`id` = `child`.`morphable_id` and `child`.`morphable_type` = 'ParentModel') as `children_count` from `parent` where (1 =  'ParentModel')
@staudenmeir
Copy link
Contributor

This affects all relationship queries with WHERE bindings.

The problem is Model::newQueryWithoutScopes() adding $withCount to every query.
So the query received by Builder::where() contains the $withCount bindings.

There are multiple issues and PRs about $withCount causing problems for all kinds of queries:
#21472, #21464, #21153, #21116, #21468, #22239

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

No branches or pull requests

3 participants