|
| 1 | +--- |
| 2 | +title: Basic resource controller |
| 3 | +sidebar_position: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +To create a basic API resource controller using this package, create a controller that extends the `Javaabu\QueryBuilder\Http\Controllers\ApiController` class. |
| 7 | + |
| 8 | +```php |
| 9 | +<?php |
| 10 | + |
| 11 | +namespace App\Http\Controllers\Api; |
| 12 | + |
| 13 | +use Javaabu\QueryBuilder\Http\Controllers\ApiController; |
| 14 | +use App\Models\Product; |
| 15 | +use Illuminate\Database\Eloquent\Builder; |
| 16 | +use Spatie\QueryBuilder\AllowedFilter; |
| 17 | + |
| 18 | +class ProductsController extends ApiController |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Get the base query |
| 22 | + * |
| 23 | + * @return Builder |
| 24 | + */ |
| 25 | + public function getBaseQuery(): Builder |
| 26 | + { |
| 27 | + return Product::query(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the allowed fields |
| 32 | + * |
| 33 | + * @return array |
| 34 | + */ |
| 35 | + public function getAllowedFields(): array |
| 36 | + { |
| 37 | + return array_diff(\Schema::getColumnListing('products'), (new Product)->getHidden()); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get the allowed includes |
| 42 | + * |
| 43 | + * @return array |
| 44 | + */ |
| 45 | + public function getAllowedIncludes(): array |
| 46 | + { |
| 47 | + return [ |
| 48 | + 'category' |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the allowed appends |
| 54 | + * |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + public function getAllowedAppends(): array |
| 58 | + { |
| 59 | + return [ |
| 60 | + 'formatted_name' => [ |
| 61 | + 'name', |
| 62 | + 'price' |
| 63 | + ], |
| 64 | + |
| 65 | + 'formatted_price' |
| 66 | + ]; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the allowed sorts |
| 71 | + * |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + public function getAllowedSorts(): array |
| 75 | + { |
| 76 | + return [ |
| 77 | + 'id', |
| 78 | + 'created_at', |
| 79 | + 'updated_at', |
| 80 | + 'slug', |
| 81 | + 'name', |
| 82 | + ]; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the default sort |
| 87 | + * |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + public function getDefaultSort(): string |
| 91 | + { |
| 92 | + return '-created_at'; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the allowed filters |
| 97 | + * |
| 98 | + * @return array |
| 99 | + */ |
| 100 | + public function getAllowedFilters(): array |
| 101 | + { |
| 102 | + return [ |
| 103 | + 'id', |
| 104 | + 'slug', |
| 105 | + 'name', |
| 106 | + AllowedFilter::scope('search'), |
| 107 | + ]; |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +The controller will have an `index` and `show` method which you can use to define your routes in your `api.php` route file. |
| 113 | + |
| 114 | +```php |
| 115 | +// in api.php route file |
| 116 | + |
| 117 | +/** |
| 118 | + * Products |
| 119 | + */ |
| 120 | +Route::get('products', [\App\Controllers\Api\ProductsController::class, 'index'])->name('products.index'); |
| 121 | +Route::get('products/{id}', [\App\Controllers\Api\ProductsController::class, 'show'])->name('products.show'); |
| 122 | +``` |
| 123 | + |
| 124 | +The following methods will have to be implemented in your controller: |
| 125 | + |
| 126 | +## getBaseQuery() |
| 127 | + |
| 128 | +This method used to define your base query. It should return a `Illuminate\Database\Eloquent\Builder` instance. |
| 129 | +This builder instance will be passed to the `Javaabu\QueryBuilder\QueryBuilder\QueryBuilder::for()` method. |
| 130 | + |
| 131 | +```php |
| 132 | +public function getBaseQuery(): Builder |
| 133 | +{ |
| 134 | + return Product::query(); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +## getAllowedFields() |
| 139 | + |
| 140 | +Used to define which fields that users will be allowed to request through the `?fields=` query parameter. |
| 141 | +If the user doesn't include the `fields` parameter, then all fields defined here will be included by default. |
| 142 | + |
| 143 | +```php |
| 144 | +public function getAllowedFields(): array |
| 145 | +{ |
| 146 | + return array_diff(\Schema::getColumnListing('products'), (new Product)->getHidden()); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +## getAllowedIncludes() |
| 151 | + |
| 152 | +Used to define which relations that users will be allowed to request through the `?include=` query parameter. |
| 153 | +If the user doesn't include the `include` parameter, then all relations defined here will be included by default. |
| 154 | +To not include any relation in a request, the user should submit a blank `?include=` parameter. |
| 155 | + |
| 156 | +```php |
| 157 | +public function getAllowedIncludes(): array |
| 158 | +{ |
| 159 | + return [ |
| 160 | + 'category' |
| 161 | + ]; |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +## getAllowedAppends() |
| 166 | + |
| 167 | +Used to define which accessor attibutes that users will be allowed to request through the `?append=` query parameter. |
| 168 | +If the user doesn't include the `append` parameter, then all appends defined here will be included by default. |
| 169 | +To not include any append in a request, the user should submit a blank `?append=` parameter. |
| 170 | + |
| 171 | +Note that append fields can also be requested through the `fields` query parameter as well. |
| 172 | + |
| 173 | +```php |
| 174 | +public function getAllowedAppends(): array |
| 175 | +{ |
| 176 | + return [ |
| 177 | + 'formatted_price' |
| 178 | + ]; |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +If an append depends on other database columns, then you can specify those fields as an array. |
| 183 | +For example, `formatted_name` of the product might be `':name (:price)`. In this case, both `name` and `price` columns are required to generate the `formatted_name`. |
| 184 | +If you do not include these columns, the user will get a blank value for `formatted_name` if they don't specifically include `name` and `price` in the fields, when doing a request like `?fields=id,formatted_name`. |
| 185 | + |
| 186 | +```php |
| 187 | +public function getAllowedAppends(): array |
| 188 | +{ |
| 189 | + return [ |
| 190 | + 'formatted_name' => [ |
| 191 | + 'name', |
| 192 | + 'price' |
| 193 | + ], |
| 194 | + ]; |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +## getAllowedSorts() |
| 199 | + |
| 200 | +Defines which fields that the user can sort the records by using the `?sort=` query param. Applies only to the `index` method. |
| 201 | +To sort in descending order, users can append a `-` to the field name, e.g. `?sort=-created_at` in the query parameter. |
| 202 | +To sort by multiple fields, users can provide a comma-separated list, e.g. `?sort=id,-created_at` |
| 203 | + |
| 204 | +```php |
| 205 | +public function getAllowedSorts(): array |
| 206 | +{ |
| 207 | + return [ |
| 208 | + 'id', |
| 209 | + 'created_at', |
| 210 | + 'updated_at', |
| 211 | + 'slug', |
| 212 | + 'name', |
| 213 | + ]; |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +## getDefaultSort() |
| 218 | + |
| 219 | +Defines the default sort to apply if the user doesn't provide any `sort` parameter. |
| 220 | + |
| 221 | +```php |
| 222 | +public function getDefaultSort(): string |
| 223 | +{ |
| 224 | + return '-created_at'; |
| 225 | +} |
| 226 | +``` |
| 227 | + |
| 228 | +Return an empty string if you don't want any default sort applied. |
| 229 | + |
| 230 | +```php |
| 231 | +public function getDefaultSort(): string |
| 232 | +{ |
| 233 | + return ''; |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +## getAllowedFilters() |
| 238 | + |
| 239 | +Defines the filters that users can apply using the `?filter[]` query parameter. |
| 240 | + |
| 241 | +```php |
| 242 | +public function getAllowedFilters(): array |
| 243 | +{ |
| 244 | + return [ |
| 245 | + 'id', |
| 246 | + 'slug', |
| 247 | + 'name', |
| 248 | + AllowedFilter::scope('search'), |
| 249 | + ]; |
| 250 | +} |
| 251 | +``` |
0 commit comments