Skip to content

Commit 4e4c1b2

Browse files
authored
Group by added (#10)
* Group by added * Apply fixes from StyleCI (#9)
1 parent 06eb7d0 commit 4e4c1b2

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ JSON parameters (keys):
5151
- ``search`` - will perform the querying logic (explained in detail [below](#search))
5252
- ``returns`` - will return only the columns provided as values.
5353
- ``order_by`` - will order the results based on values provided.
54+
- ``group_by`` - will group the results based on values provided.
5455
- ``relations`` - will load the relations for the given model.
5556
- `limit` - will limit the results returned.
5657
- `offset` - will return a subset of results starting from a point given. This parameter **MUST**
@@ -185,6 +186,25 @@ Example:
185186

186187
Will perform a ``SELECT ... ORDER BY first_name asc, last_name desc``
187188

189+
### Group by
190+
191+
Using ``group_by`` key does an 'group by' based on the given key(s). Order of the keys
192+
matters!
193+
194+
Arguments are presumed to be a single attribute or array of attributes.
195+
196+
Since group by behaves like it would in a plain SQL query, be sure to select
197+
the right fields and aggregate functions.
198+
199+
Example:
200+
```
201+
{
202+
"group_by": ["last_name", "first_name"]
203+
}
204+
```
205+
206+
Will perform a ``SELECT ... GROUP BY last_name, first_name``
207+
188208
### Relations
189209

190210
It is possible to load object relations as well by using ``relations`` parameter.

config/asseco-json-query-builder.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Asseco\JsonQueryBuilder\RequestParameters\CountParameter;
4+
use Asseco\JsonQueryBuilder\RequestParameters\GroupByParameter;
45
use Asseco\JsonQueryBuilder\RequestParameters\LimitParameter;
56
use Asseco\JsonQueryBuilder\RequestParameters\OffsetParameter;
67
use Asseco\JsonQueryBuilder\RequestParameters\OrderByParameter;
@@ -30,6 +31,7 @@
3031
LimitParameter::class,
3132
OffsetParameter::class,
3233
CountParameter::class,
34+
GroupByParameter::class,
3335
],
3436

3537
/**

src/RequestParameters/CountParameter.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Asseco\JsonQueryBuilder\RequestParameters;
66

77
use Asseco\JsonQueryBuilder\Exceptions\JsonQueryBuilderException;
8-
use Illuminate\Support\Facades\DB;
98

109
class CountParameter extends AbstractParameter
1110
{
@@ -27,6 +26,6 @@ public function areArgumentsValid(): void
2726

2827
public function appendQuery(): void
2928
{
30-
$this->builder->select(DB::raw('count(*) as count'));
29+
$this->builder->selectRaw('count(*) as count');
3130
}
3231
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Asseco\JsonQueryBuilder\RequestParameters;
6+
7+
class GroupByParameter extends AbstractParameter
8+
{
9+
public static function getParameterName(): string
10+
{
11+
return 'group_by';
12+
}
13+
14+
public function appendQuery(): void
15+
{
16+
$this->builder->groupBy($this->arguments);
17+
}
18+
}

src/RequestParameters/RelationsParameter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function appendComplexRelation(array $argument): void
4242
$relation = key($argument);
4343
$input = $argument[$relation];
4444

45-
$this->builder->with([$relation => function ($query) use ($input) {
45+
$this->builder->with([Str::camel($relation) => function ($query) use ($input) {
4646
$jsonQuery = new JsonQuery($query->getQuery(), $input);
4747
$jsonQuery->search();
4848
}]);

0 commit comments

Comments
 (0)