Skip to content

Commit f50e5fc

Browse files
authored
Merge pull request #75 from Samoht70/aggregate-alias-support
Aggregate alias support
2 parents d9a5903 + 3a3cac3 commit f50e5fc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

content/2.endpoints/2.search.md

+22
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Here is a quick look at what you can do:
5858
"relation": "stars",
5959
"type": "max",
6060
"field": "rate",
61+
"alias": "approved_max_stars",
6162
"filters": [
6263
{"field": "approved", "value": true}
6364
]
@@ -104,6 +105,7 @@ Here is a quick look at what you can do:
104105
| `aggregates.relation` | `string` | X | | The relation you are querying |
105106
| `aggregates.type` | `string` | X | | The type of the aggregates you want to use in: `min`, `max`, `avg`, `sum`, `count` and `exists` |
106107
| `aggregates.field` | `string` | when type is not `exists` or `count` | `*` | The field you want to execute your aggregate on |
108+
| `aggregates.alias` | `string` | | | The alias given for the aggregate |
107109
| `aggregates.filters` | `array` | | | You can specify all the arguments for the `filters` section |
108110
| **Instructions** | | | | |
109111
| `instructions.name` | `string` | X | | The instruction `uriKey` |
@@ -416,6 +418,26 @@ The type could be one of these:
416418
For the `exists` and `count` operation you must not specify the field since these aggregates don't base themselves on a column.
417419
::
418420

421+
### Aliases
422+
423+
You may optionally define an alias for your aggregate using the `alias` method. This allows you to customize the name of the aggregate column in the response, instead of relying on Laravel's default naming.
424+
425+
```json
426+
// (POST) api/posts/search
427+
{
428+
"search": {
429+
"aggregates": [
430+
{
431+
"relation": "comments",
432+
"type": "avg",
433+
"field": "stars",
434+
"alias": "average_stars"
435+
}
436+
]
437+
}
438+
}
439+
```
440+
419441
#### Aggregates filtering
420442

421443
For more complex aggregates, Laravel Rest Api allows you to specify filters. These filters are the same as [the basic ones](/endpoints/search#filters).

0 commit comments

Comments
 (0)