Skip to content

Commit 52ff26f

Browse files
committed
Aggregate alias support
1 parent d9a5903 commit 52ff26f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

content/2.endpoints/2.search.md

Lines changed: 22 additions & 0 deletions
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_rate",
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 name to use for the aggregate column in the response |
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": "stars_average"
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)