8000 [Docs] Add documentation on basic filtering (#189) · tekord/laravel-json-api@0a98b07 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 17, 2023. It is now read-only.

Commit 0a98b07

Browse files
GIANTCRABlindyhopchris
authored andcommitted
[Docs] Add documentation on basic filtering (cloudcreativity#189)
1 parent b91c1e5 commit 0a98b07

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

docs/fetching/filtering.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
11
# Filtering
22

3-
@todo
3+
This package supports the specification of [JSON API Fetching of Filtered Resources](http://jsonapi.org/format/1.0/#fetching-filtering). Filtering allows requests to remove certain resources not required.
4+
5+
# Using Filter Parameter
6+
7+
Per the specification, the client is able to request for the inclusion of related resources through the following HTTP request:
8+
9+
```http
10+
GET /api/posts?filter[approved]=1 HTTP/2.0
11+
Accept: application/vnd.api+json
12+
```
13+
14+
However, by default, this package denies filtering attributes via parameter and would throw the follow error message.
15+
16+
```json
17+
{
18+
"errors": [
19+
{
20+
"title": "Filter should contain only allowed values.",
21+
"source": {
22+
"parameter": "filter"
23+
}
24+
}
25+
]
26+
}
27+
28+
```
29+
30+
To allow certain attributes to be filtered using request parameters, it must be manually enabled through the resource's `Validators.php` file.
31+
32+
```php
33+
namespace App\JsonApiV1\Posts;
34+
35+
class Validators extends AbstractValidatorProvider
36+
{
37+
protected $allowedFilteringParameters = [
38+
'approved'
39+
];
40+
41+
/* Your code here ...*/
42+
}
43+
44+
```
45+
46+
And then to put the filtering into action, you add the following part to your `Adapter.php` file.
47+
48+
```php
49+
class Adapter extends AbstractAdapter
50+
{
51+
/**
52+
* @param $query
53+
* @param Collection $filters
54+
* @return mixed
55+
*/
56+
protected function filter($query, Collection $filters)
57+
{
58+
foreach ($filters as $filter_key => $filter_value) {
59+
if ($filter_key === 'approved') {
60+
$query->where($filter_key, $filter_value);
61+
}
62+
}
63+
return $query;
64+
}
65+
}
66+
```

docs/fetching/inclusion.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Inclusion
21

3-
## Introduction
2+
# Introduction
43

54
This package supports the specification of [JSON API Inclusion of Related Resources](http://jsonapi.org/format/1.0/#fetching-includes). This allows you to load all the data of the specified resources that is bounded by relationship.
65

7-
### Using Include Parameter
6+
# Using Include Parameter
87

98
Per the specification, the client is able to request for the inclusion of related resources through the following HTTP request:
109

@@ -45,14 +44,14 @@ class Validators extends AbstractValidatorProvider
4544
```
4645

4746

48-
### Auto Inclusion
47+
# Auto Inclusion
4948

5049
It is possible to force the inclusion of certain related resources; displaying it even when the client did not specify the related resource in the include parameter. To enable the automatic inclusion of resources, edit the resource's `Schema.php` file.
5150

5251
```php
5352
namespace App\JsonApiV1\Posts;
5453

55-
class Schema extends EloquentSchema
54+
class Schema extends SchemaProvider
5655
{
5756

5857
/* Your code here ...*/

0 commit comments

Comments
 (0)
0