File tree Expand file tree Collapse file tree 5 files changed +42
-3
lines changed Expand file tree Collapse file tree 5 files changed +42
-3
lines changed Original file line number Diff line number Diff 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
186187Will 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
190210It is possible to load object relations as well by using `` relations `` parameter.
Original file line number Diff line number Diff line change 11<?php
22
33use Asseco \JsonQueryBuilder \RequestParameters \CountParameter ;
4+ use Asseco \JsonQueryBuilder \RequestParameters \GroupByParameter ;
45use Asseco \JsonQueryBuilder \RequestParameters \LimitParameter ;
56use Asseco \JsonQueryBuilder \RequestParameters \OffsetParameter ;
67use Asseco \JsonQueryBuilder \RequestParameters \OrderByParameter ;
3031 LimitParameter::class,
3132 OffsetParameter::class,
3233 CountParameter::class,
34+ GroupByParameter::class,
3335 ],
3436
3537 /**
Original file line number Diff line number Diff line change 55namespace Asseco \JsonQueryBuilder \RequestParameters ;
66
77use Asseco \JsonQueryBuilder \Exceptions \JsonQueryBuilderException ;
8- use Illuminate \Support \Facades \DB ;
98
109class 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}
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
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 }]);
You can’t perform that action at this time.
0 commit comments