Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Sep 16, 2024
1 parent 77e3fc7 commit 6d7a868
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Grammars/QueryBuilderGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function packQueryBuilder(\Illuminate\Database\Query\Builder $builder)
'from' => $builder->from,
'wheres' => $this->packWheres($builder->wheres),
'groups' => $builder->groups,
'havings' => $builder->havings,
'havings' => $this->packWheres($builder->havings),
'groupLimit' => $builder->groupLimit ?? null,
'orders' => $builder->orders,
'limit' => $builder->limit,
Expand All @@ -42,7 +42,7 @@ protected function packQueryBuilder(\Illuminate\Database\Query\Builder $builder)
protected function unpackQueryBuilder(array $data, \Illuminate\Database\Query\Builder $builder): \Illuminate\Database\Query\Builder
{
foreach ($data as $key => $value) {
if ($key == 'wheres') {
if (in_array($key, ['wheres', 'havings'])) {
$value = $this->unpackWheres($value, $builder);
}

Expand Down Expand Up @@ -70,6 +70,10 @@ protected function unpackQueryBuilder(array $data, \Illuminate\Database\Query\Bu
*/
private function packWheres($wheres)
{
if (is_null($wheres)) {
return $wheres;
}

foreach ($wheres as &$item) {
if (isset($item['query'])) {
$item['query'] = $this->packQueryBuilder($item['query']);
Expand Down Expand Up @@ -128,6 +132,10 @@ private function packJoins($joins)
*/
private function unpackWheres($wheres, \Illuminate\Database\Query\Builder $builder)
{
if (is_null($wheres)) {
return $wheres;
}

foreach ($wheres as &$item) {
if (isset($item['query'])) {
$item['query'] = $this->unpackQueryBuilder($item['query'], $builder->newQuery());
Expand Down
17 changes: 17 additions & 0 deletions tests/HavingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,21 @@ public function testSimple()
// Raw
$this->compare(User::groupBy('id')->havingRaw('COUNT(id) > ?', [1]));
}

/**
* @return void
*/
public function testComplex()
{
// One level
$this->compare(User::groupBy('id')->having(fn ($query) => $query->havingRaw('COUNT(id) > ?', [1])));

// Two levels
$this->compare(
User::groupBy('id')
->having(function ($query) {
$query->having(fn ($query) => $query->havingRaw('COUNT(id) > ?', [1]));
})
);
}
}

0 comments on commit 6d7a868

Please sign in to comment.