diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index b629dc7f6..e89c0800d 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -309,7 +309,7 @@ public function eagerLoadRelations(array $models) ->where($withAggregate['constraints']) ->whereIn($withAggregate['relation']->getForeignKeyName(), $modelIds) ->groupBy($withAggregate['relation']->getForeignKeyName()) - ->aggregate($withAggregate['function'], $withAggregate['column'] ?? [$withAggregate['relation']->getPrimaryKeyName()]); + ->aggregate($withAggregate['function'], [$withAggregate['column']]); foreach ($models as $model) { $value = $withAggregate['function'] === 'count' ? 0 : null; diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 3998bb65e..cfd2907ae 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -28,6 +28,7 @@ use Override; use RuntimeException; use stdClass; +use TypeError; use function array_fill_keys; use function array_is_list; @@ -335,7 +336,7 @@ public function toMql(): array if ($this->aggregate) { $function = $this->aggregate['function']; - foreach ((array) $this->aggregate['columns'] as $column) { + foreach ($this->aggregate['columns'] as $column) { // Add unwind if a subdocument array should be aggregated // column: subarray.price => {$unwind: '$subarray'} $splitColumns = explode('.*.', $column); @@ -344,7 +345,7 @@ public function toMql(): array $column = implode('.', $splitColumns); } - $aggregations = blank($this->aggregate['columns']) ? [] : (array) $this->aggregate['columns']; + $aggregations = blank($this->aggregate['columns']) ? [] : $this->aggregate['columns']; if (in_array('*', $aggregations) && $function === 'count' && empty($group['_id'])) { $options = $this->inheritConnectionOptions($this->options); @@ -484,11 +485,11 @@ public function getFresh($columns = [], $returnLazy = false) // here to either the passed columns, or the standard default of retrieving // all of the columns on the table using the "wildcard" column character. if ($this->columns === null) { - $this->columns = (array) $columns; + $this->columns = $columns; } // Drop all columns if * is present, MongoDB does not work this way. - if (in_array('*', (array) $this->columns)) { + if (in_array('*', $this->columns)) { $this->columns = []; } @@ -556,6 +557,8 @@ public function generateCacheKey() /** @return ($function is null ? AggregationBuilder : mixed) */ public function aggregate($function = null, $columns = ['*']) { + assert(is_array($columns), new TypeError(sprintf('Argument #2 ($columns) must be of type array, %s given', get_debug_type($columns)))); + if ($function === null) { if (! trait_exists(FluentFactoryTrait::class)) { // This error will be unreachable when the mongodb/builder package will be merged into mongodb/mongodb