1 | <?php |
||||||
2 | |||||||
3 | namespace VGirol\JsonApi\Controllers; |
||||||
4 | |||||||
5 | use Illuminate\Http\JsonResponse; |
||||||
6 | use Illuminate\Http\Request; |
||||||
7 | use Illuminate\Support\Facades\DB; |
||||||
8 | use VGirol\JsonApi\Resources\ResourceObject; |
||||||
9 | use VGirol\JsonApiConstant\Members; |
||||||
10 | |||||||
11 | trait CanStoreResource |
||||||
12 | { |
||||||
13 | /** |
||||||
14 | * Undocumented function |
||||||
15 | * |
||||||
16 | * @param Request $request |
||||||
17 | * @param string $routeKey |
||||||
18 | * @param mixed $parentId |
||||||
19 | * |
||||||
20 | * @return JsonResponse |
||||||
21 | */ |
||||||
22 | protected function storeAndReply(Request $request, string $routeKey, $parentId = null): JsonResponse |
||||||
23 | { |
||||||
24 | // Creates and validate specific FormRequest |
||||||
25 | $request = $this->validateData($request); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
26 | |||||||
27 | // Stores resource |
||||||
28 | $resource = DB::transaction(function () use ($request, $routeKey, $parentId) { |
||||||
29 | return $this->storeSingle($request, $routeKey, $parentId); |
||||||
30 | }, config('jsonapi.transactionAttempts')); |
||||||
31 | |||||||
32 | // Fills response's content |
||||||
33 | $return204 = $request->has(Members::DATA . '.' . Members::ID) && !$resource->haveChanged(); |
||||||
34 | $response = $return204 ? $this->responseService->noContent() : $this->responseService->created($resource); |
||||||
35 | |||||||
36 | // Adds Location header |
||||||
37 | if (config('jsonapi.creationAddLocationHeader')) { |
||||||
38 | $response->header('Location', $resource->getResourceSelfLink($request)); |
||||||
39 | } |
||||||
40 | |||||||
41 | return $response; |
||||||
42 | } |
||||||
43 | |||||||
44 | /** |
||||||
45 | * Store a single resource |
||||||
46 | * |
||||||
47 | * @param Request $request |
||||||
48 | * @param string $routeKey |
||||||
49 | * @param int $parentId |
||||||
50 | * |
||||||
51 | * @return ResourceObject |
||||||
52 | */ |
||||||
53 | protected function storeSingle(Request $request, string $routeKey, $parentId = null) |
||||||
54 | { |
||||||
55 | // Gets the data |
||||||
56 | // $data = $request->validated(Members::DATA); |
||||||
57 | $data = $request->input(Members::DATA); |
||||||
0 ignored issues
–
show
The method
input() does not exist on Illuminate\Http\Request .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
58 | |||||||
59 | // store resource |
||||||
60 | $model = $this->storeService->saveModel($data, $routeKey); |
||||||
61 | |||||||
62 | // Looks for relationships |
||||||
63 | $this->relationshipService->saveAll($data, $model); |
||||||
64 | |||||||
65 | // Attach created resource (if related) to parent |
||||||
66 | if ($parentId !== null) { |
||||||
67 | // Get relation instance |
||||||
68 | $relation = $this->fetchService->getRelationFromRequest($request, $parentId, $routeKey); |
||||||
69 | |||||||
70 | // Attach resource to parent |
||||||
71 | $this->relationshipService->create($relation, $model); |
||||||
72 | } |
||||||
73 | |||||||
74 | // Get model for response (with required include or fields) |
||||||
75 | $requiredModel = $this->fetchService->getRequiredModel($request, $model->getKey(), $model); |
||||||
76 | |||||||
77 | // Creates resource |
||||||
78 | $resource = $this->exportService->exportSingleResource($requiredModel); |
||||||
79 | |||||||
80 | return $resource; |
||||||
81 | } |
||||||
82 | } |
||||||
83 |