1 | <?php |
||
26 | abstract class JsonApiController extends Controller |
||
27 | { |
||
28 | use JsonApiErrors; |
||
29 | use QueriesResources; |
||
30 | use UpdatesModelRelations; |
||
31 | use AuthorizesRequests; |
||
32 | use ValidatesRequests; |
||
33 | |||
34 | /** |
||
35 | * The Eloquent Model for the resource. |
||
36 | * |
||
37 | * @var Model|string |
||
38 | */ |
||
39 | protected $model; |
||
40 | |||
41 | /** |
||
42 | * Create a new JsonApiController instance. |
||
43 | */ |
||
44 | public function __construct() |
||
55 | |||
56 | /** |
||
57 | * Return a listing of the resource. |
||
58 | * |
||
59 | * @param Request $request |
||
60 | * @param \Illuminate\Database\Eloquent\Builder|null $query Custom resource query |
||
61 | * |
||
62 | * @return JsonApiResponse |
||
63 | */ |
||
64 | public function indexAction(Request $request, $query = null) |
||
84 | |||
85 | /** |
||
86 | * Store a new record. |
||
87 | * |
||
88 | * @param Request $request |
||
89 | * |
||
90 | * @return JsonApiResponse |
||
91 | */ |
||
92 | public function storeAction(Request $request) |
||
102 | |||
103 | /** |
||
104 | * Return a specified record. |
||
105 | * |
||
106 | * @param Request $request |
||
107 | * @param Model|mixed $record |
||
108 | * |
||
109 | * @return JsonApiResponse |
||
110 | */ |
||
111 | public function showAction(Request $request, $record) |
||
119 | |||
120 | /** |
||
121 | * Update a specified record. |
||
122 | * |
||
123 | * @param Request $request |
||
124 | * @param Model|mixed $record |
||
125 | * |
||
126 | * @return JsonApiResponse |
||
127 | */ |
||
128 | public function updateAction(Request $request, $record) |
||
140 | |||
141 | /** |
||
142 | * Destroy a specified record. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * @param Model|mixed $record |
||
146 | * |
||
147 | * @return JsonApiResponse |
||
148 | */ |
||
149 | public function destroyAction(Request $request, $record) |
||
156 | |||
157 | /** |
||
158 | * Return a specified record relationship. |
||
159 | * |
||
160 | * @param Request $request |
||
161 | * @param Model|mixed $record |
||
162 | * @param string $relation |
||
163 | * |
||
164 | * @return JsonApiResponse |
||
165 | */ |
||
166 | public function showRelationshipAction(Request $request, $record, $relation) |
||
172 | |||
173 | /** |
||
174 | * Update a named relationship on a specified record. |
||
175 | * |
||
176 | * http://jsonapi.org/format/#crud-updating-relationships |
||
177 | * |
||
178 | * @param Request $request |
||
179 | * @param Model|mixed $record |
||
180 | * @param string $relation |
||
181 | * |
||
182 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
183 | * |
||
184 | * @return JsonApiResponse |
||
185 | */ |
||
186 | public function updateRelationshipAction(Request $request, $record, $relation) |
||
187 | { |
||
188 | $record = $this->findModelInstance($record); |
||
189 | $relationType = $this->getRelationType($relation); |
||
190 | |||
191 | abort_unless(is_string($relationType) && $this->isFillableRelation($relation), Response::HTTP_NOT_FOUND); |
||
192 | |||
193 | if ($relationType === 'To-One') { |
||
194 | $this->updateToOneResourceRelationship($record, $relation, $request->input('data')); |
||
195 | } else if ($relationType === 'To-Many') { |
||
196 | $this->updateToManyResourceRelationship($record, $relation, $request->input('data'), $request->method()); |
||
197 | } |
||
198 | |||
199 | return new JsonApiResponse(new RelationshipSerializer($record, $relation)); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Return existing instance of the resource or find by primary key. |
||
204 | * |
||
205 | * @param Model|mixed $record |
||
206 | * |
||
207 | * @throws ModelNotFoundException |
||
208 | * |
||
209 | * @return Model |
||
210 | */ |
||
211 | protected function findModelInstance($record) |
||
212 | { |
||
213 | if ($record instanceof Model) { |
||
214 | if (is_null($record->getKey())) { |
||
215 | throw new ModelNotFoundException(); |
||
216 | } |
||
217 | |||
218 | return $record; |
||
219 | } |
||
220 | |||
221 | return $this->model->findOrFail($record); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Return any JSON API resource parameters from a request. |
||
226 | * |
||
227 | * @param Request $request |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | protected function getRequestParameters($request) |
||
246 | |||
247 | /** |
||
248 | * Return any comma separated values in a request query field as an array. |
||
249 | * |
||
250 | * @param Request $request |
||
251 | * @param string $key |
||
252 | * @param string|null $validate Regular expression to test for each item |
||
253 | * |
||
254 | * @throws \Illuminate\Validation\ValidationException |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | protected function getRequestQuerySet($request, $key, $validate = null) |
||
275 | |||
276 | /** |
||
277 | * Validate the requested included relationships against those that are |
||
278 | * allowed on the requested resource type. |
||
279 | * |
||
280 | * @param array|null $relations |
||
281 | * |
||
282 | * @throws InvalidRelationPathException |
||
283 | */ |
||
284 | protected function validateIncludableRelations($relations) |
||
296 | } |
||
297 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: