Skip to content

Commit 8ebd6bb

Browse files
committed
Validate $columns parameter is not set when creating an agg builder
1 parent defb21f commit 8ebd6bb

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/Query/Builder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,10 @@ public function generateCacheKey()
600600
public function aggregate($function = null, $columns = [])
601601
{
602602
if ($function === null) {
603+
if ($columns !== []) {
604+
throw new InvalidArgumentException('Columns cannot be specified to create an aggregation builder. Add a $project stage instead.');
605+
}
606+
603607
return $this->getAggregationBuilder();
604608
}
605609

tests/Query/BuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,12 @@ public static function provideExceptions(): iterable
18761876
'First argument of MongoDB\Laravel\Query\Builder::where must be a field path as "string". Got "float"',
18771877
fn (Builder $builder) => $builder->where(2.3, '>', 1),
18781878
];
1879+
1880+
yield 'aggregate with columns' => [
1881+
InvalidArgumentException::class,
1882+
'Columns cannot be specified to create an aggregation builder. Add a $project stage instead.',
1883+
fn (Builder $builder) => $builder->aggregate(null, ['foo']),
1884+
];
18791885
}
18801886

18811887
/** @dataProvider getEloquentMethodsNotSupported */

0 commit comments

Comments
 (0)