'WithCount do not run subquery condition
I have a model Organization which has relation to the Client model:
public function clients() { return $this->hasMany(Client::class); }
Client table and model have two important columns for this query organization_id and status. I need to make a list of organizations with a count of active clients. Here is my query:
$query = Organization::withCount(['clients as no_of_clients' => function ($query) { $query->where('clients.status', 'active'); }])->groupBy('organizations.id')->get()
but I get no_of_clients as a sum of all clients, an additional filter where status = 'active' didn't run. Can someone explain to me why and how to make it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
