Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Added query builder support for groupByRaw #31498

Merged
merged 4 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ class Builder
* @var array
*/
public $bindings = [
'select' => [],
'from' => [],
'join' => [],
'where' => [],
'having' => [],
'order' => [],
'union' => [],
'select' => [],
'from' => [],
'join' => [],
'where' => [],
'groupBy' => [],
'having' => [],
'order' => [],
'union' => [],
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
'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 @@ -1045,6 +1045,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