beyondcode / beyondcode/laravel-query-detector

False detection when using count?

Open
#101 2 comments 0 reactions 0 assignees View on GitHub
good first issue
Dominant language
PHP
Stars
2.1k
Forks
144
PR merge metrics
No merged PRs in 30d

Description

I am using the following code to count a user's open tasks:
```php
$tasks_count = Auth::user()->tasks()->whereCompleted(false)->count();
```
The plugin tells me:
```
Model: App\Models\User => Relation: App|Models\Task - You should add "with ('App\Models\Task')" to eager-load this relation.
Model: App\Models\Task => Relation: user - You should add
"with ('user')" to eager-load this relation.
```
When I do that, the issue still occurs:
```php
$tasks_count = Auth::user()->tasks()->with('user')->whereCompleted(false)->count();
```
Using
```php
$tasks_count = Auth::user()->with('tasks')->tasks()->whereCompleted(false)->count();
```
gives me `Call to undefined method Illuminate\Database\Eloquent\Builder::tasks()` but user has a tasks relationship:
```php
class User extends Authenticatable
{
// ...

public function tasks()
{
return $this->hasMany('App\Models\Task');
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.