|
| 1 | +--- |
| 2 | +title: Additional method overrides |
| 3 | +sidebar_position: 2 |
| 4 | +--- |
| 5 | + |
| 6 | +Apart from the required methods that have to be implemented, API controllers also include other methods than can be overriden. |
| 7 | + |
| 8 | +## modifyModel() |
| 9 | + |
| 10 | +This method can be used to set additional attributes or hide specific attributes on the model before returning the results. |
| 11 | + |
| 12 | +```php |
| 13 | +public function modifyModel(Model $model): Model |
| 14 | +{ |
| 15 | + $model->setHidden('media'); |
| 16 | + |
| 17 | + $model->some_random_fake_attribute = 'some value'; |
| 18 | + |
| 19 | + return $model; |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +## authorizeView() |
| 24 | + |
| 25 | +Set authorization on the show endpoint. |
| 26 | + |
| 27 | +```php |
| 28 | +protected function authorizeView(Model $model): void |
| 29 | +{ |
| 30 | + $this->authorize('view', $model); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## getFieldsToAlwaysInclude |
| 35 | + |
| 36 | +Specifies which fields to always include, regardless of what the user provides in the `?fields=` query parameter. |
| 37 | +By default this is set to return the `id` field. |
| 38 | + |
| 39 | +```php |
| 40 | +public function getFieldsToAlwaysInclude(): array |
| 41 | +{ |
| 42 | + return [ |
| 43 | + 'id' |
| 44 | + ]; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## getIndexAllowedFields |
| 49 | + |
| 50 | +Override this method if you want to allow different fields just for the `index` endpoint. |
| 51 | + |
| 52 | +```php |
| 53 | +public function getIndexAllowedFields(): array |
| 54 | +{ |
| 55 | + return $this->getAllowedFields(); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## getIndexValidation |
| 60 | + |
| 61 | +Override this method to specify any validation rules to run for the `index` endpoint. |
| 62 | +The default validation rules are given below. |
| 63 | + |
| 64 | +```php |
| 65 | +protected function getIndexValidation(): array |
| 66 | +{ |
| 67 | + return [ |
| 68 | + 'per_page' => "integer|" . ($this->allowUnlimitedResultsPerPage() ? 'min:-1' : 'between:1,' . $this->getMaxPerPage()), |
| 69 | + 'page' => 'integer|min:1', |
| 70 | + ]; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## getShowValidation |
| 75 | + |
| 76 | +Override this method to specify any validation rules to run for the `show` endpoint. |
| 77 | +The default validation rules are given below. |
| 78 | + |
| 79 | +```php |
| 80 | +protected function getShowValidation(): array |
| 81 | +{ |
| 82 | + return []; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## getShowAllowedAppends |
| 87 | + |
| 88 | +Override this method if you want to specify specific `appends` just for the `show` endpoint. |
| 89 | +These will be merged with the `getAllowedAppends`. |
| 90 | + |
| 91 | +```php |
| 92 | +public function getShowAllowedAppends(): array |
| 93 | +{ |
| 94 | + return []; |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## getAllShowAllowedAppends |
| 99 | + |
| 100 | +Override this method if you want to specify specific `appends` just for the `show` endpoint without merging with the `getAllowedAppends`. |
| 101 | + |
| 102 | +```php |
| 103 | +public function getAllShowAllowedAppends(): array |
| 104 | +{ |
| 105 | + return [ |
| 106 | + 'formatted_name', |
| 107 | + ]; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
0 commit comments