File tree 5 files changed +42
-3
lines changed
5 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ JSON parameters (keys):
51
51
- `` search `` - will perform the querying logic (explained in detail [ below] ( #search ) )
52
52
- `` returns `` - will return only the columns provided as values.
53
53
- `` order_by `` - will order the results based on values provided.
54
+ - `` group_by `` - will group the results based on values provided.
54
55
- `` relations `` - will load the relations for the given model.
55
56
- ` limit ` - will limit the results returned.
56
57
- ` offset ` - will return a subset of results starting from a point given. This parameter ** MUST**
@@ -185,6 +186,25 @@ Example:
185
186
186
187
Will perform a `` SELECT ... ORDER BY first_name asc, last_name desc ``
187
188
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
+
188
208
### Relations
189
209
190
210
It is possible to load object relations as well by using `` relations `` parameter.
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
3
use Asseco \JsonQueryBuilder \RequestParameters \CountParameter ;
4
+ use Asseco \JsonQueryBuilder \RequestParameters \GroupByParameter ;
4
5
use Asseco \JsonQueryBuilder \RequestParameters \LimitParameter ;
5
6
use Asseco \JsonQueryBuilder \RequestParameters \OffsetParameter ;
6
7
use Asseco \JsonQueryBuilder \RequestParameters \OrderByParameter ;
30
31
LimitParameter::class,
31
32
OffsetParameter::class,
32
33
CountParameter::class,
34
+ GroupByParameter::class,
33
35
],
34
36
35
37
/**
Original file line number Diff line number Diff line change 5
5
namespace Asseco \JsonQueryBuilder \RequestParameters ;
6
6
7
7
use Asseco \JsonQueryBuilder \Exceptions \JsonQueryBuilderException ;
8
- use Illuminate \Support \Facades \DB ;
9
8
10
9
class CountParameter extends AbstractParameter
11
10
{
@@ -27,6 +26,6 @@ public function areArgumentsValid(): void
27
26
28
27
public function appendQuery (): void
29
28
{
30
- $ this ->builder ->select ( DB :: raw ( 'count(*) as count ' ) );
29
+ $ this ->builder ->selectRaw ( 'count(*) as count ' );
31
30
}
32
31
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ protected function appendComplexRelation(array $argument): void
42
42
$ relation = key ($ argument );
43
43
$ input = $ argument [$ relation ];
44
44
45
- $ this ->builder ->with ([$ relation => function ($ query ) use ($ input ) {
45
+ $ this ->builder ->with ([Str:: camel ( $ relation) => function ($ query ) use ($ input ) {
46
46
$ jsonQuery = new JsonQuery ($ query ->getQuery (), $ input );
47
47
$ jsonQuery ->search ();
48
48
}]);
You can’t perform that action at this time.
0 commit comments