Skip to content

Commit 8ac211f

Browse files
committed
wip
1 parent c829dd2 commit 8ac211f

File tree

1 file changed

+113
-16
lines changed

1 file changed

+113
-16
lines changed

docs/docs/3.0/filtering/filtering.md

Lines changed: 113 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Filtering entities
22

3-
Laravel Restify provides configurable and powerful way of filtering over entities.
4-
5-
All of your repositories has search/filtering out of the box. You just have to specify the configuration for that.
3+
Laravel Restify provides configurable and powerful way of filtering over entities.
64

75
## Search
86

@@ -15,25 +13,27 @@ class PostRepository extends Repository
1513
public static $search = ['id', 'title'];
1614
```
1715

18-
Now the `Post` entity is searchable by `id` and `title`, so you could use `search` query param for filtering the index
16+
Now `posts` are searchable by `id` and `title`, so you could use `search` query param for filtering the index
1917
request:
2018

2119
```http request
2220
GET: /restify-api/posts?search="Test title"
2321
```
2422

25-
## Match attribute
23+
## Match
24+
25+
Matching by specific attributes may be useful if you want an exact matching.
2626

27-
Matching by specific attributes may be useful if you want an exact matching. Repository
28-
configuration:
27+
Repository configuration:
2928

3029
```php
3130
class PostRepository extends Repository
3231
{
3332
public static $match = [
34-
'id' => Binaryk\LaravelRestify\Contracts\RestifySearchable::MATCH_INTEGER,
35-
'title' => Binaryk\LaravelRestify\Contracts\RestifySearchable::MATCH_TEXT,
36-
];
33+
'id' => RestifySearchable::MATCH_INTEGER
34+
'title' => RestifySearchable::MATCH_TEXT,
35+
];
36+
}
3737
```
3838

3939
As we may notice the match configuration is an associative array, defining the attribute name and type mapping.
@@ -43,6 +43,8 @@ Available types:
4343
- text (or `string`)
4444
- bool
4545
- int (or `integer`)
46+
- datetime
47+
- array
4648

4749
When performing the request you may pass the match field and value as query params:
4850

@@ -56,21 +58,116 @@ or by title:
5658
GET: /restify-api/posts?title="Some title"
5759
```
5860

61+
### Match datetime
62+
63+
The `datetime` filter add behind the scene an `whereDate` query.
64+
65+
```php
66+
class PostRepository extends Repository
67+
{
68+
public static $match = [
69+
'published_at' => RestifySearchable::MATCH_DATETIME,
70+
];
71+
}
72+
```
73+
74+
Request:
75+
76+
```http request
77+
GET: /restify-api/posts?published_at=2020-12-01
78+
```
79+
80+
### Match null
81+
82+
Match accept `null` as a value, and check add `whereNull` to the query:
83+
84+
```http request
85+
GET: /restify-api/posts?published_at=null
86+
```
87+
88+
### Match array
89+
90+
Match also accept a list of elements in the query param:
91+
92+
```php
93+
class PostRepository extends Repository
94+
{
95+
public static $match = [
96+
'id' => RestifySearchable::MATCH_ARRAY
97+
];
98+
}
99+
```
100+
101+
Request:
102+
103+
```http request
104+
GET: /restify-api/posts?id=1,2,3
105+
```
106+
107+
This will be converted to:
108+
109+
```php
110+
->whereIn('id', [1, 2, 3])
111+
```
112+
113+
### Match negation
114+
115+
You can negate the column match by simply adding the `-` (minus) sign before the field:
116+
117+
```http request
118+
GET: /restify-api/posts?-id=1,2,3
119+
```
120+
121+
This will return all posts where doesn't have the `id` in the `[1,2,3]` list.
122+
123+
You can apply `-` (negation) for every match:
124+
125+
```http request
126+
GET: /restify-api/posts?-title="Some title"
127+
```
128+
129+
This will return all posts that doesn't contain `Some title` substring.
130+
131+
### Match closure
132+
133+
There may be situations when the filter you want to apply not necessarily is a database attributes. You can use a Closure to handle this easily:
134+
135+
```php
136+
// UserRepository
137+
public static function getMatchByFields()
138+
{
139+
return [
140+
'is_active' => function ($request, $query) {
141+
if ($request->boolean('is_active')) {
142+
$query->whereNotNull('email_verified_at');
143+
} else {
144+
$query->whereNull('email_verified_at');
145+
}
146+
}
147+
];
148+
}
149+
```
150+
151+
So now you can query this:
152+
153+
```http request
154+
GET: /restify-api/users?is_active=true
155+
```
59156

60157
## Sort
61158
When index query entities, usually we have to sort by specific attributes.
62159

63160
This requires the `$sort` configuration:
64161

65162
```php
66-
class PostRepository extends Repository
163+
class PostRepository extends Repository
67164
{
68165
public static $sort = ['id'];
69166
```
70167

71168
Performing request requires the sort query param:
72169

73-
Sorting DESC requires a minus sign before the attribute name:
170+
Sorting DESC requires a minus (`-`) sign before the attribute name:
74171

75172
```http request
76173
GET: /restify-api/posts?sort=-id
@@ -88,19 +185,19 @@ or with plus sign before the field:
88185
GET: /restify-api/posts?sort=+id
89186
```
90187

91-
## Eager loading - aka related
188+
## Eager loading - aka withs
92189

93190
When get a repository index or details about a single entity, often we have to get the related entities (we have access to).
94-
This eager loading is configurable by Restify as:
191+
This eager loading is configurable by Restify as follow:
95192

96193
```php
97-
public static $related = ['user'];
194+
public static $related = ['posts'];
98195
```
99196

100197
This means that we could use `posts` query for eager loading posts:
101198

102199
```http request
103-
GET: /restify-api/related?related=user
200+
GET: /restify-api/users?with=posts
104201
```
105202

106203
## Pagination

0 commit comments

Comments
 (0)