diff --git a/src/db/concern/ModelRelationQuery.php b/src/db/concern/ModelRelationQuery.php index e6d92531..61ce197a 100644 --- a/src/db/concern/ModelRelationQuery.php +++ b/src/db/concern/ModelRelationQuery.php @@ -468,7 +468,7 @@ public function withCache(string | array | bool $relation = true, $key = true, $ */ public function withCount(string | array $relation, bool $subQuery = true) { - return $this->withAggregate($relation, 'count', '*', $subQuery); + return $this->withAggregate($relation, 'count', '', $subQuery); } /** diff --git a/src/model/concern/RelationShip.php b/src/model/concern/RelationShip.php index 6b2ff2bf..016f2e6d 100644 --- a/src/model/concern/RelationShip.php +++ b/src/model/concern/RelationShip.php @@ -428,6 +428,8 @@ public function relationCount(Query $query, array $relations, string $aggregate $relation = Str::camel($relation); + $field = $field ?: $this->$relation()->getPk(); + if ($useSubQuery) { $count = $this->$relation()->getRelationCountQuery($closure, $aggregate, $field, $name); } else {