2323use Illuminate \Contracts \Validation \Validator ;
2424use Illuminate \Database \Eloquent \Model ;
2525use Illuminate \Http \Response ;
26- use Illuminate \Support \Collection ;
2726use LaravelJsonApi \Contracts \Auth \Authorizer ;
2827use LaravelJsonApi \Contracts \Resources \Container as ResourceContainer ;
2928use LaravelJsonApi \Core \Document \ResourceObject ;
4241class ResourceRequest extends FormRequest
4342{
4443
44+ /**
45+ * @var bool
46+ */
47+ protected bool $ validateExisting = true ;
48+
4549 /**
4650 * @var callable|null
4751 */
@@ -101,7 +105,7 @@ public function isUpdating(): bool
101105 *
102106 * @return bool
103107 */
104- public function isUpdatingRelation (): bool
108+ public function isUpdatingRelationship (): bool
105109 {
106110 return $ this ->isMethod ('PATCH ' ) && $ this ->isRelationship ();
107111 }
@@ -111,7 +115,7 @@ public function isUpdatingRelation(): bool
111115 *
112116 * @return bool
113117 */
114- public function isAttachingRelation (): bool
118+ public function isAttachingRelationship (): bool
115119 {
116120 return $ this ->isMethod ('POST ' ) && $ this ->isRelationship ();
117121 }
@@ -121,7 +125,7 @@ public function isAttachingRelation(): bool
121125 *
122126 * @return bool
123127 */
124- public function isDetachingRelation (): bool
128+ public function isDetachingRelationship (): bool
125129 {
126130 return $ this ->isMethod ('DELETE ' ) && $ this ->isRelationship ();
127131 }
@@ -133,7 +137,9 @@ public function isDetachingRelation(): bool
133137 */
134138 public function isModifyingRelationship (): bool
135139 {
136- return $ this ->isUpdatingRelation () || $ this ->isAttachingRelation () || $ this ->isDetachingRelation ();
140+ return $ this ->isUpdatingRelationship () ||
141+ $ this ->isAttachingRelationship () ||
142+ $ this ->isDetachingRelationship ();
137143 }
138144
139145 /**
@@ -164,11 +170,11 @@ public function authorizeResource(Authorizer $authorizer): bool
164170 $ model = $ this ->modelOrFail ();
165171 $ fieldName = $ this ->jsonApi ()->route ()->fieldName ();
166172
167- if ($ this ->isAttachingRelation ()) {
173+ if ($ this ->isAttachingRelationship ()) {
168174 return $ authorizer ->attachRelationship ($ this , $ model , $ fieldName );
169175 }
170176
171- if ($ this ->isDetachingRelation ()) {
177+ if ($ this ->isDetachingRelationship ()) {
172178 return $ authorizer ->detachRelationship ($ this , $ model , $ fieldName );
173179 }
174180
@@ -198,16 +204,6 @@ public function document(): array
198204 return $ document ;
199205 }
200206
201- /**
202- * Get the field name for a relationship request.
203- *
204- * @return string
205- */
206- public function fieldName (): string
207- {
208- return $ this ->jsonApi ()->route ()->fieldName ();
209- }
210-
211207 /**
212208 * @inheritDoc
213209 */
@@ -275,7 +271,7 @@ public function validationDataForDelete(): array
275271 public function validatedForRelation ()
276272 {
277273 $ data = $ this ->validated ();
278- $ fieldName = $ this ->fieldName ();
274+ $ fieldName = $ this ->getFieldName ();
279275
280276 if (array_key_exists ($ fieldName , $ data )) {
281277 return $ data [$ fieldName ];
@@ -388,8 +384,14 @@ protected function createDeleteValidator(ValidationFactory $factory): Validator
388384 return $ factory ->make (
389385 $ this ->validationDataForDelete (),
390386 method_exists ($ this , 'deleteRules ' ) ? $ this ->container ->call ([$ this , 'deleteRules ' ]) : [],
391- method_exists ($ this , 'deleteMessages ' ) ? $ this ->deleteMessages () : $ this ->messages (),
392- method_exists ($ this , 'deleteAttributes ' ) ? $ this ->deleteAttributes () : $ this ->attributes ()
387+ array_merge (
388+ $ this ->messages (),
389+ method_exists ($ this , 'deleteMessages ' ) ? $ this ->deleteMessages () : []
390+ ),
391+ array_merge (
392+ $ this ->attributes (),
393+ method_exists ($ this , 'deleteAttributes ' ) ? $ this ->deleteAttributes () : []
394+ )
393395 );
394396 }
395397
@@ -418,24 +420,24 @@ protected function dataForCreate(array $document): array
418420 * merge attributes provided by the client over the top of the existing attribute
419421 * values.
420422 *
421- * @param Model|object $record
422- * the record being updated.
423+ * @param Model|object $model
424+ * the model being updated.
423425 * @param array $document
424426 * the JSON API document to validate.
425427 * @return array
426428 */
427- protected function dataForUpdate (object $ record , array $ document ): array
429+ protected function dataForUpdate (object $ model , array $ document ): array
428430 {
429431 $ data = $ document ['data ' ] ?? [];
430432
431- if ($ this ->mustValidateExisting ($ record , $ data )) {
433+ if ($ this ->mustValidateExisting ($ model , $ document )) {
432434 $ data ['attributes ' ] = $ this ->extractAttributes (
433- $ record ,
435+ $ model ,
434436 $ data ['attributes ' ] ?? []
435437 );
436438
437439 $ data ['relationships ' ] = $ this ->extractRelationships (
438- $ record ,
440+ $ model ,
439441 $ data ['relationships ' ] ?? []
440442 );
441443 }
@@ -575,7 +577,7 @@ private function extractRelationships(object $model, array $new): array
575577 private function relationshipRules (): array
576578 {
577579 $ rules = $ this ->container ->call ([$ this , 'rules ' ]);
578- $ fieldName = $ this ->fieldName ();
580+ $ fieldName = $ this ->getFieldName ();
579581
580582 return collect ($ rules )
581583 ->filter (fn ($ v , $ key ) => Str::startsWith ($ key , $ fieldName ))
0 commit comments