You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/14.database/05.data-sprunjing/docs.md
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,11 +18,11 @@ GET http://example.com/api/users?size=5&page=0&sorts%5Bname%5D=asc
18
18
19
19
This tells the server that we want a list of users sorted by their `name`, in ascending order (`sorts[name]=asc`). We then want them chunked into blocks of 5 results at a time (`size=5`), and for this request we want the first chunk (`page=0`).
20
20
21
-
Building queries that can handle all of these request parameters correctly, for every one of your endpoints, can be surprisingly tricky and very tedious. This is why UserFrosting introduces the **Sprunjer**, allowing you to easily define rules for how clients can retrieve constrained results from your database.
21
+
Building queries that can handle all of these request parameters correctly, for every one of your endpoints, can be surprisingly tricky and very tedious. This is why UserFrosting introduces the **Sprunje**, allowing you to easily define rules for how clients can retrieve constrained results from your database.
22
22
23
23
## Sprunje parameters
24
24
25
-
Every Sprunje can accept the following parameters. Typically, they are passed in as a query string in a `GET` request, and then passed as the second argument in your Sprunje's constructor.
25
+
Every Sprunje can accept the following parameters. Typically, they are passed in the query string of a `GET` request, then passed along as the second argument of your Sprunje's constructor.
26
26
27
27
-`sorts`: an associative array of field names mapped to sort directions. Sort direction can be either `asc` or `desc`.
28
28
-`filters`: an associative array of field names mapped to queries. For example, `name: Attenb` will search for names that contain "Attenb."
@@ -34,7 +34,7 @@ Every Sprunje can accept the following parameters. Typically, they are passed in
34
34
35
35
By implementing a custom `Sprunje` class, you can pass parameters directly from the client request and they will be used to safely construct the appropriate query.
36
36
37
-
A custom Sprunje is simply a class that extends the base `UserFrosting\Sprinkle\Core\Sprunje\Sprunje` class. At the minimum you must implement the `baseQuery`class, which specifies the Eloquent query to be performed before any additional constraints are applied. By convention, you should place your Sprunje classes in `src/Sprunje/` in your Sprinkle.
37
+
A custom Sprunje is simply a class that extends the base `UserFrosting\Sprinkle\Core\Sprunje\Sprunje` class. At the minimum you must implement the `baseQuery`function, which specifies the Eloquent query to be performed before any additional constraints are applied. By convention, you should place your Sprunje classes in `src/Sprunje/` in your Sprinkle.
38
38
39
39
**OwlSprunje.php**
40
40
@@ -242,6 +242,8 @@ protected function applyTransformations(Collection $collection): Collection
242
242
}
243
243
```
244
244
245
+
[notice=warning]There are a few methods that do _not_ automatically apply transformations. Specifically `getColumnValues`--and methods such as [the default `getListable`](#sprunje-lists) which use `getColumnValues` as their datasource--skip this step. If your Sprunje depends on transformations, you may wish to override `getColumnValues` and/or use custom methods for all `listable` fields.[/notice]
246
+
245
247
## Extending a Sprunje query
246
248
247
249
You might want to further modify your Sprunje's base query in certain endpoints. For example, you might want to retrieve the owner of each Owl in your result set, but only in your `/api/owls` endpoint. In this case, you can modify your Sprunje by passing a callback to the `extendQuery` method:
@@ -299,6 +301,8 @@ An array mapping each listable field to a list of possible values can be obtaine
299
301
300
302
[notice=warning]It is recommended to use strings for both the `value` and `text` for compatibility purposes with the TableSorter plugin. You can cast your value to a string by wrapping it in double quotation marks or with `(string)` prefix of the value. See [this thread](https://github.com/userfrosting/UserFrosting/issues/966#issuecomment-483245033) for an example.[/notice]
301
303
304
+
[notice]Remember that `getListable` does not apply [transformations](#custom-data-transformations). If your Sprunje uses transformations, consider writing custom methods for each listable field. [/notice]
305
+
302
306
Of course you can override the default listing behavior for a field by defining a custom method. This method must consist of the field name (converted to StudlyCase) prefixed with `list`:
0 commit comments