Skip to content

Commit

Permalink
Illuminate\Database\Query\Builder: added support to groupByRaw($sql, …
Browse files Browse the repository at this point in the history
…array $bindings = []) [fixes #1112].
  • Loading branch information
rentalhost committed Feb 16, 2020
1 parent cfd068e commit d50c82d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ class Builder
* @var array
*/
public $bindings = [
'select' => [],
'from' => [],
'join' => [],
'where' => [],
'having' => [],
'order' => [],
'union' => [],
'unionOrder' => [],
'select' => [],
'from' => [],
'join' => [],
'where' => [],
'groupBy' => [],
'having' => [],
'order' => [],
'union' => [],
'unionOrder' => []
];

/**
Expand Down Expand Up @@ -1695,6 +1696,21 @@ public function groupBy(...$groups)
return $this;
}

/**
* Add a raw groupBy clause to the query.
*
* @param string $sql
* @param array $bindings
*/
public function groupByRaw($sql, array $bindings = []): self
{
$this->groups[] = new Expression($sql);

$this->addBinding($bindings, 'groupBy');

return $this;
}

/**
* Add a "having" clause to the query.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,15 @@ public function testGroupBys()
$builder = $this->getBuilder();
$builder->select('*')->from('users')->groupBy(new Raw('DATE(created_at)'));
$this->assertSame('select * from "users" group by DATE(created_at)', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->groupByRaw('DATE(created_at), ? DESC', ['foo']);
$this->assertSame('select * from "users" group by DATE(created_at), ? DESC', $builder->toSql());
$this->assertEquals(['foo'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->havingRaw('?', ['havingRawBinding'])->groupByRaw('?', ['groupByRawBinding'])->whereRaw('?', ['whereRawBinding']);
$this->assertEquals(['whereRawBinding', 'groupByRawBinding', 'havingRawBinding'], $builder->getBindings());
}

public function testOrderBys()
Expand Down

0 comments on commit d50c82d

Please sign in to comment.