Skip to content

Commit b72017d

Browse files
committed
Validate arg type and avoid subsequent error
1 parent 62a4ff8 commit b72017d

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/Eloquent/Builder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function eagerLoadRelations(array $models)
309309
->where($withAggregate['constraints'])
310310
->whereIn($withAggregate['relation']->getForeignKeyName(), $modelIds)
311311
->groupBy($withAggregate['relation']->getForeignKeyName())
312-
->aggregate($withAggregate['function'], $withAggregate['column'] ?? [$withAggregate['relation']->getPrimaryKeyName()]);
312+
->aggregate($withAggregate['function'], [$withAggregate['column']]);
313313

314314
foreach ($models as $model) {
315315
$value = $withAggregate['function'] === 'count' ? 0 : null;

src/Query/Builder.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Override;
2929
use RuntimeException;
3030
use stdClass;
31+
use TypeError;
3132

3233
use function array_fill_keys;
3334
use function array_is_list;
@@ -335,7 +336,7 @@ public function toMql(): array
335336
if ($this->aggregate) {
336337
$function = $this->aggregate['function'];
337338

338-
foreach ((array) $this->aggregate['columns'] as $column) {
339+
foreach ($this->aggregate['columns'] as $column) {
339340
// Add unwind if a subdocument array should be aggregated
340341
// column: subarray.price => {$unwind: '$subarray'}
341342
$splitColumns = explode('.*.', $column);
@@ -344,7 +345,7 @@ public function toMql(): array
344345
$column = implode('.', $splitColumns);
345346
}
346347

347-
$aggregations = blank($this->aggregate['columns']) ? [] : (array) $this->aggregate['columns'];
348+
$aggregations = blank($this->aggregate['columns']) ? [] : $this->aggregate['columns'];
348349

349350
if (in_array('*', $aggregations) && $function === 'count' && empty($group['_id'])) {
350351
$options = $this->inheritConnectionOptions($this->options);
@@ -484,11 +485,11 @@ public function getFresh($columns = [], $returnLazy = false)
484485
// here to either the passed columns, or the standard default of retrieving
485486
// all of the columns on the table using the "wildcard" column character.
486487
if ($this->columns === null) {
487-
$this->columns = (array) $columns;
488+
$this->columns = $columns;
488489
}
489490

490491
// Drop all columns if * is present, MongoDB does not work this way.
491-
if (in_array('*', (array) $this->columns)) {
492+
if (in_array('*', $this->columns)) {
492493
$this->columns = [];
493494
}
494495

@@ -556,6 +557,8 @@ public function generateCacheKey()
556557
/** @return ($function is null ? AggregationBuilder : mixed) */
557558
public function aggregate($function = null, $columns = ['*'])
558559
{
560+
assert(is_array($columns), new TypeError(sprintf('Argument #2 ($columns) must be of type array, %s given', get_debug_type($columns))));
561+
559562
if ($function === null) {
560563
if (! trait_exists(FluentFactoryTrait::class)) {
561564
// This error will be unreachable when the mongodb/builder package will be merged into mongodb/mongodb

0 commit comments

Comments
 (0)