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
Generate with `php artisan make:policy PostPolicy --model=Post`
52
52
53
-
-Get list - calls the `viewAny` policy
54
-
-Get single - calls the `view` policy
55
-
-Post New - calls the `create` policy
56
-
-Put Update - calls the `update` policy
57
-
-Delete item - calls the `delete` policy
53
+
- Get list - calls the `viewAny` policy
54
+
- Get single - calls the `view` policy
55
+
- Post New - calls the `create` policy
56
+
- Put Update - calls the `update` policy
57
+
- Delete item - calls the `delete` policy
58
58
59
59
Query/Data modifiers in policies for the api endpoints
60
60
61
-
-`qualifyCollectionQueryWithUser($user, $repository)` -> return void - add any queries to the repository (ie ->where('x','))
62
-
-`qualifyItemQueryWithUser($user, $repository)`-> return void - add any queries to the repository (ie ->where('x','))
63
-
-`qualifyStoreDataWithUser($data)` - return the updated data array
64
-
-`qualifyUpdateDataWithUser($data)` - return the updated data array
61
+
-`qualifyCollectionQueryWithUser($user, $repository)` -> return void - add any queries to the repository (ie ->where('x','))
62
+
-`qualifyItemQueryWithUser($user, $repository)`-> return void - add any queries to the repository (ie ->where('x','))
63
+
-`qualifyStoreDataWithUser($data)` - return the updated data array
64
+
-`qualifyUpdateDataWithUser($data)` - return the updated data array
65
65
66
66
## Resources / Collections (Transforming)
67
67
@@ -84,8 +84,8 @@ in your controller override the following params:
84
84
85
85
## Snake vs Camel
86
86
87
-
-middleware to convert all camel to snake: `Phpsa\LaravelApiController\Http\Middleware\SnakeCaseInputs`
88
-
-set request header `X-Accept-Case-Type` to either `snake` or `camel` to alter your data response
87
+
- middleware to convert all camel to snake: `Phpsa\LaravelApiController\Http\Middleware\SnakeCaseInputs`
88
+
- set request header `X-Accept-Case-Type` to either `snake` or `camel` to alter your data response
89
89
90
90
## Filtering
91
91
@@ -137,15 +137,15 @@ You can easily filter using any related model that is configured for `include`.
137
137
138
138
By default all fields are returned, you can limit that to specific fields in the following ways:
139
139
140
-
-Api Controller parameter `$defaultFields` default as `protected $defaultFields = ['*'];` - switch to include an array of fields
141
-
-fields param in url querystring: ie `fields=id,name,age` = will only return those, this will also override the above.
142
-
-in your response resource you can set the static::allowedFields to lock down which fields are returnable
143
-
-`addfields` and `removefields` params in url querystring will work with these.
144
-
-Use laravel [eloquent model `$appends`](https://laravel.com/docs/6.x/eloquent-serialization#appending-values-to-json) property to automatically include custom attribute accessors.
140
+
- Api Controller parameter `$defaultFields` default as `protected $defaultFields = ['*'];` - switch to include an array of fields
141
+
- fields param in url querystring: ie `fields=id,name,age` = will only return those, this will also override the above.
142
+
- in your response resource you can set the static::allowedFields to lock down which fields are returnable
143
+
-`addfields` and `removefields` params in url querystring will work with these.
144
+
- Use laravel [eloquent model `$appends`](https://laravel.com/docs/6.x/eloquent-serialization#appending-values-to-json) property to automatically include custom attribute accessors.
145
145
146
146
## Relationships
147
147
148
-
-Using the relationships defined in your models, you can pass a comma delimited list eg `include=join1,join2` which will return those joins (one or many).
148
+
- Using the relationships defined in your models, you can pass a comma delimited list eg `include=join1,join2` which will return those joins (one or many).
149
149
150
150
Simply add a `protected static $mapResources` to your `Resource` to define which resources to assign your related data. E.e., for a one to many relationship, you should specify a collection, and a one-to-one relationship specify the related resource directly. This will allow the API to properly format the related record.
151
151
@@ -158,24 +158,24 @@ Simply add a `protected static $mapResources` to your `Resource` to define which
158
158
159
159
- You can automatically update and create related records for most types of relationships. Just include the related resource name in your POST or PUT request.
160
160
161
-
For `BelongsToMany` or `MorphToMany` relationships, you can choose the sync strategy. By default, this will take an *additive* strategy. That is to say, related records sent will be ADDED to any existing related records. On a request-by-request basis, you can opt for a *sync* strategy which will remove the pivot for any related records not listed in the request. Note the actual related record will not be removed, just the pivot entry.
161
+
For `BelongsToMany` or `MorphToMany` relationships, you can choose the sync strategy. By default, this will take an _additive_ strategy. That is to say, related records sent will be ADDED to any existing related records. On a request-by-request basis, you can opt for a _sync_ strategy which will remove the pivot for any related records not listed in the request. Note the actual related record will not be removed, just the pivot entry.
162
162
163
-
To opt for the *sync* behavaiour, set `?sync[field]=true` in your request.
163
+
To opt for the _sync_ behavaiour, set `?sync[field]=true` in your request.
164
164
165
165
## Sorting
166
166
167
-
-Sorts can be passed as comma list aswell, ie `sort=age asc` or `sort=age asc,name desc,eyes` - generates sql of `sort age asc` and `sort age asc, name desc, eyes asc` respectively
168
-
-Default sort can also be added on the controller using by overrideing the `protected $defaultSort = null;` parameter
167
+
- Sorts can be passed as comma list aswell, ie `sort=age asc` or `sort=age asc,name desc,eyes` - generates sql of `sort age asc` and `sort age asc, name desc, eyes asc` respectively
168
+
- Default sort can also be added on the controller using by overrideing the `protected $defaultSort = null;` parameter
169
169
170
170
## Pagination
171
171
172
-
-pagination can be enabled/disbled on the controller by overriding the `protected $defaultLimit = 25;` on the controller
173
-
-pagination can also be passed via the url using `limit=xx&page=y`
174
-
-pagination can also be limited to a max per page by overriding the `protected $maximumLimit = false;` parameter
172
+
- pagination can be enabled/disbled on the controller by overriding the `protected $defaultLimit = 25;` on the controller
173
+
- pagination can also be passed via the url using `limit=xx&page=y`
174
+
- pagination can also be limited to a max per page by overriding the `protected $maximumLimit = false;` parameter
175
175
176
176
## Validation
177
177
178
-
-When Posting a new record, validation can be done by adding a `rulesForCreate` method to your controller returning an array eg
178
+
- When Posting a new record, validation can be done by adding a `rulesForCreate` method to your controller returning an array eg
179
179
180
180
```php
181
181
[
@@ -186,22 +186,22 @@ To opt for the *sync* behavaiour, set `?sync[field]=true` in your request.
186
186
187
187
see https://laravel.com/docs/5.8/validation#conditionally-adding-rules
188
188
189
-
-for updating a record, add a method `rulesForUpdate` per above.
189
+
- for updating a record, add a method `rulesForUpdate` per above.
190
190
191
191
## Defaults
192
192
193
193
The following parameters are set in the Base Api controller and can be overwritten by your Controller on a case by case basis:
-`protected $resourceSingle = JsonResource::class;` Collection to use for your single resource
199
-
-`protected $resourceCollection = ResourceCollection::class;` Collection to use for your resource collection
200
-
-`protected $defaultFields = ['*'];` Default Fields to respond with
201
-
-`protected $defaultSort = null;` Set the default sorting for queries.
202
-
-`protected $defaultLimit = 25;` Number of items displayed at once if not specified. (0 = maximumLimit)
203
-
-`protected $maximumLimit = 0;` Maximum limit that can be set via \$\_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
204
-
-`protected $unguard = false;` Do we need to unguard the model before create/update?
198
+
-`protected $resourceSingle = JsonResource::class;` Collection to use for your single resource
199
+
-`protected $resourceCollection = ResourceCollection::class;` Collection to use for your resource collection
200
+
-`protected $defaultFields = ['*'];` Default Fields to respond with
201
+
-`protected $defaultSort = null;` Set the default sorting for queries.
202
+
-`protected $defaultLimit = 25;` Number of items displayed at once if not specified. (0 = maximumLimit)
203
+
-`protected $maximumLimit = 0;` Maximum limit that can be set via \$\_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
204
+
-`protected $unguard = false;` Do we need to unguard the model before create/update?
205
205
206
206
## Scopes
207
207
@@ -222,21 +222,31 @@ class MyModelResource extends ApiResource
222
222
223
223
you can now append `withTrashed=1` or `onlyTrashed=1` to your query.
224
224
225
+
## Responses
226
+
227
+
you can override responses for each point by overriding the following protected methods:
228
+
229
+
- handleIndexResponse
230
+
- handleStoreResponse
231
+
- handleShowResponse
232
+
- handleUpdateResponse
233
+
- handleDestroyResponse
234
+
225
235
## Security
226
236
227
237
If you discover any security related issues, please email
0 commit comments