1 | <?php |
||
13 | abstract class JsonApiController extends Controller |
||
14 | { |
||
15 | use JsonApiErrors; |
||
16 | |||
17 | /** |
||
18 | * Return the Eloquent Model for the resource. |
||
19 | * |
||
20 | * @return Model |
||
21 | */ |
||
22 | abstract protected function getModel(); |
||
23 | |||
24 | /** |
||
25 | * Return the type name of the resource. |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | protected function getModelType() |
||
33 | |||
34 | /** |
||
35 | * Return the names of relationships that can be updated. |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | protected function getModelRelationships() |
||
43 | |||
44 | /** |
||
45 | * Return a listing of the resource. |
||
46 | * |
||
47 | * @param Request $request |
||
48 | * |
||
49 | * @return JsonApiResponse |
||
50 | */ |
||
51 | public function indexAction(Request $request) |
||
83 | |||
84 | /** |
||
85 | * Store a new record. |
||
86 | * |
||
87 | * @param Request $request |
||
88 | * |
||
89 | * @return JsonApiResponse |
||
90 | */ |
||
91 | public function storeAction(Request $request) |
||
97 | |||
98 | /** |
||
99 | * Return a specified record. |
||
100 | * |
||
101 | * @param Request $request |
||
102 | * @param Model|int $record |
||
103 | * |
||
104 | * @return JsonApiResponse |
||
105 | */ |
||
106 | public function showAction(Request $request, $record) |
||
113 | |||
114 | /** |
||
115 | * Update a specified record. |
||
116 | * |
||
117 | * @param Request $request |
||
118 | * @param Model|int $record |
||
119 | * |
||
120 | * @return JsonApiResponse |
||
121 | */ |
||
122 | public function updateAction(Request $request, $record) |
||
129 | |||
130 | /** |
||
131 | * Destroy a specified record. |
||
132 | * |
||
133 | * @param Request $request |
||
134 | * @param Model|int $record |
||
135 | * |
||
136 | * @return JsonApiResponse |
||
137 | */ |
||
138 | public function destroyAction(Request $request, $record) |
||
145 | |||
146 | /** |
||
147 | * Update a named many-to-one relationship association on a specified record. |
||
148 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
149 | * |
||
150 | * @param Request $request |
||
151 | * @param Model|int $record |
||
152 | * @param string $relation |
||
153 | * @param string|null $foreignKey |
||
154 | * |
||
155 | * @return JsonApiResponse |
||
156 | */ |
||
157 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
170 | |||
171 | /** |
||
172 | * Update named many-to-many relationship entries on a specified record. |
||
173 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
174 | * |
||
175 | * @param Request $request |
||
176 | * @param Model|int $record |
||
177 | * @param string $relation |
||
178 | * |
||
179 | * @return JsonApiResponse |
||
180 | */ |
||
181 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
208 | |||
209 | /** |
||
210 | * Return an instance of the resource by primary key. |
||
211 | * |
||
212 | * @param int $key |
||
213 | * |
||
214 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
215 | * |
||
216 | * @return Model |
||
217 | */ |
||
218 | protected function findModelInstance($key) |
||
222 | |||
223 | /** |
||
224 | * Return any JSON API resource parameters from a request. |
||
225 | * |
||
226 | * @param Request $request |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function getRequestParameters($request) |
||
239 | |||
240 | /** |
||
241 | * Return any comma separated values in a request query field as an array. |
||
242 | * |
||
243 | * @param Request $request |
||
244 | * @param string $key |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | protected function getRequestQuerySet($request, $key) |
||
252 | |||
253 | /** |
||
254 | * Transform a set of models into a JSON API collection. |
||
255 | * |
||
256 | * @param \Illuminate\Support\Collection $records |
||
257 | * @param array $fields |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function transformCollection($records, array $fields = []) |
||
271 | |||
272 | /** |
||
273 | * Transform a model instance into a JSON API object. |
||
274 | * |
||
275 | * @param Model $record |
||
276 | * @param array|null $fields Field names of attributes to limit to |
||
277 | * @param array|null $include Relations to include |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | protected function transformRecord($record, array $fields = [], array $include = []) |
||
322 | } |
||
323 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.