Skip to content

Commit

Permalink
[6.x] Illuminate\Database\Query\Builder: added support to groupByRaw(…
Browse files Browse the repository at this point in the history
…$sql, array $bindings = []) [fixes #1112]. (#31498)

* Illuminate\Database\Query\Builder: added support to groupByRaw($sql, array $bindings = []) [fixes #1112].

* Update Builder.php

* Update Builder.php

* Update Builder.php

Co-authored-by: Graham Campbell <GrahamCampbell@users.noreply.github.com>
  • Loading branch information
rentalhost and GrahamCampbell authored Feb 18, 2020
1 parent 55fc26e commit a1dc376
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ class Builder
*/
public $bindings = [
'select' => [],
'from' => [],
'join' => [],
'where' => [],
'from' => [],
'join' => [],
'where' => [],
'groupBy' => [],
'having' => [],
'order' => [],
'union' => [],
'order' => [],
'union' => [],
'unionOrder' => [],
];

Expand Down Expand Up @@ -1685,6 +1686,22 @@ public function groupBy(...$groups)
return $this;
}

/**
* Add a raw groupBy clause to the query.
*
* @param string $sql
* @param array $bindings
* @return $this
*/
public function groupByRaw($sql, array $bindings = [])
{
$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 a1dc376

Please sign in to comment.