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
This will return all posts where doesn't have the `id` in the `[1,2,3]` list.
130
129
131
-
You can apply `-` (negation) for every match:
130
+
You can apply `-` (negation) for every match:
132
131
133
132
```http request
134
133
GET: /api/restify/posts?-title="Some title"
@@ -138,7 +137,9 @@ This will return all posts that doesn't contain `Some title` substring.
138
137
139
138
### Match closure
140
139
141
-
There may be situations when the filter you want to apply not necessarily is a database attributes. In your `booted` method you can add more filters for the `$match` where the key represents the field used as query param, and value should be a `Closure` which gets the request and current query `Builder`:
140
+
There may be situations when the filter you want to apply not necessarily is a database attributes. In your `booted`
141
+
method you can add more filters for the `$match` where the key represents the field used as query param, and value
142
+
should be a `Closure` which gets the request and current query `Builder`:
142
143
143
144
```php
144
145
// UserRepository
@@ -154,16 +155,18 @@ protected static function booted()
154
155
}
155
156
```
156
157
157
-
So now you can query this:
158
+
So now you can query this:
158
159
159
160
```http request
160
161
GET: /api/restify/users?active=true
161
162
```
162
163
163
-
164
164
### Matchable
165
165
166
-
Sometimes you may have a large logic into a match `Closure`, and the booted method could become a mess. To prevent this, `Restify` provides a declarative way to define `matchers`. For this purpose you should define a class, and implement the `Binaryk\LaravelRestify\Repositories\Matchable` contract. You can use the following command to generate that:
166
+
Sometimes you may have a large logic into a match `Closure`, and the booted method could become a mess. To prevent
167
+
this, `Restify` provides a declarative way to define `matchers`. For this purpose you should define a class, and
168
+
implement the `Binaryk\LaravelRestify\Repositories\Matchable` contract. You can use the following command to generate
169
+
that:
167
170
168
171
```shell script
169
172
php artisan restify:match ActiveMatch
@@ -203,26 +206,26 @@ You can use the following request to get all repository matches:
203
206
/api/restify/posts/filters?only=matches
204
207
```
205
208
206
-
## Sort
207
-
When index query entities, usually we have to sort by specific attributes.
208
-
This requires the `$sort` configuration:
209
+
## Sort
210
+
211
+
When index query entities, usually we have to sort by specific attributes. This requires the `$sort` configuration:
209
212
210
213
```php
211
214
class PostRepository extends Repository
212
215
{
213
216
public static $sort = ['id'];
214
217
```
215
-
216
-
Performing request requires the sort query param:
217
-
218
-
Sorting DESC requires a minus (`-`) sign before the attribute name:
219
-
218
+
219
+
Performing request requires the sort query param:
220
+
221
+
Sorting DESC requires a minus (`-`) sign before the attribute name:
222
+
220
223
```http request
221
224
GET: /api/restify/posts?sort=-id
222
225
```
223
226
224
-
Sorting ASC:
225
-
227
+
Sorting ASC:
228
+
226
229
```http request
227
230
GET: /api/restify/posts?sort=id
228
231
```
@@ -233,22 +236,58 @@ or with plus sign before the field:
233
236
GET: /api/restify/posts?sort=+id
234
237
```
235
238
239
+
### Sort using BelongsTo
240
+
241
+
Sometimes you may need to sort by a `belongsTo` relationship. This become a breeze with Restify. Firstly you have to
242
+
instruct your sort to use a relationship:
243
+
244
+
```php
245
+
// PostRepository
246
+
use Binaryk\LaravelRestify\Fields\BelongsTo;
247
+
use Binaryk\LaravelRestify\Filters\SortableFilter;
Make sure that the column is fully qualified (include the table name).
262
+
263
+
The request could look like:
264
+
265
+
```http request
266
+
GET: /api/restify/posts?sort=-users.name
267
+
```
268
+
269
+
This will return all posts, sorted descending by users name.
270
+
271
+
:::tip Set column optional
272
+
As you may notice we have typed twice the `users.name` (on the array key, and as argument in the `setColumn` method). As soon as you use the fully qualified key name, you can avoid the `setColumn` call, since the column will be injected automatically based on the `sorts` key.
273
+
:::
274
+
236
275
### Get available sorts
237
276
238
-
You can use the following request to get sortable attributes for a repository:
277
+
You can use the following request to get sortable attributes for a repository:
239
278
240
279
```http request
241
280
/api/restify/posts/filters?only=sortables
242
281
```
243
282
244
-
:::tip All filters
245
-
You can use `/api/restify/posts/filters?only=sortables` request, and concatenate: `?only=sortables,matches, searchables` to get all of them at once.
283
+
:::tip All filters You can use `/api/restify/posts/filters?only=sortables` request, and
284
+
concatenate: `?only=sortables,matches, searchables` to get all of them at once.
246
285
:::
247
286
248
287
## Eager loading - aka withs
249
288
250
-
When get a repository index or details about a single entity, often we have to get the related entities (we have access to).
251
-
This eager loading is configurable by Restify as following:
289
+
When get a repository index or details about a single entity, often we have to get the related entities (we have access
290
+
to). This eager loading is configurable by Restify as following:
You are not limited to add only relations under the `related` array. You can use whatever you want, for instance you can return a simple model, or a collection. Basically any serializable data could be added there. For example:
266
-
304
+
You are not limited to add only relations under the `related` array. You can use whatever you want, for instance you can
305
+
return a simple model, or a collection. Basically any serializable data could be added there. For example:
267
306
268
307
```php
269
308
public static $related = [
270
309
'foo'
271
310
];
272
311
```
273
312
274
-
Then in the `Post` model we can define this method as:
313
+
Then in the `Post` model we can define this method as:
275
314
276
315
```php
277
316
public function foo() {
@@ -281,7 +320,8 @@ public function foo() {
281
320
282
321
### Custom data format
283
322
284
-
You can use a custom related cast class (aka transformer). You can do so by modifying the `restify.casts.related` property. The default related cast is `Binaryk\LaravelRestify\Repositories\Casts\RelatedCast`.
323
+
You can use a custom related cast class (aka transformer). You can do so by modifying the `restify.casts.related`
324
+
property. The default related cast is `Binaryk\LaravelRestify\Repositories\Casts\RelatedCast`.
285
325
286
326
The cast class should extends the `Binaryk\LaravelRestify\Repositories\Casts\RepositoryCast` abstract class.
0 commit comments