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

loadCount don't work correctly with withTrashed-columns #114

Closed
nickurt opened this issue Mar 18, 2021 · 3 comments
Closed

loadCount don't work correctly with withTrashed-columns #114

nickurt opened this issue Mar 18, 2021 · 3 comments

Comments

@nickurt
Copy link

nickurt commented Mar 18, 2021

Hey!,

  • Laravel 8.32.1
  • PHP 7.3
  • eloquent-has-many-deep 1.13.1

The loadCount-function don't work with withTrashed because those columns will be not removed from the QueryBuilder

class QuxController extends Controller
{
    public function show(Qux $qux)
    {
        $qux->loadCount('fooBarBaz');
    }
}

class Qux extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
    use \Illuminate\Database\Eloquent\SoftDeletes;

    public function fooBarBaz()
    {
        return $this->hasManyDeep(Foo::class, [Bar::class, Baz::class], ['qux_id', 'baz_id', 'bar_id'], ['id', 'id', 'id'])
            ->withTrashed(['bar.deleted_at', 'baz.deleted_at']);
    }
}

will generate a query like this

select `id`, 
    (select count(*) from `foo` 
        inner join `bar` on `bar`.`id` = `foo`.`bar_id` 
        inner join `baz` on `baz`.`id` = `bar`.`baz_id` 
        where `bar`.`deleted_at` is null and `baz`.`deleted_at` is null and `qux`.`id` = `baz`.`qux_id`
    ) as `foo_bar_baz_count` 
from `qux` where `qux`.`id` in (?)

while I expected the bar.deleted_at and baz.deleted_at should be deleted just like the with function is doing ...

Is there a way to solve this?

@staudenmeir
Copy link
Owner

I'll look into it.

@hasnat-we
Copy link

@nickurt I found sadly that, Laravel removes additional constraints like 'distinct' clause from the relationship query when we use the relation in eager count. Seems like Laravel also removes 'withTrashed' clause.

@staudenmeir
Copy link
Owner

I released a new version that fixes this issue.

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