@@ -123,8 +123,8 @@ public function updateAction(Request $request, $record)
123
123
$ record ->fill ((array ) $ request ->input ('data.attributes ' ));
124
124
$ record ->save ();
125
125
126
- if ($ relationships = $ request ->input ('data.relationships ' )) {
127
- $ this ->updateRecordRelationships ($ record , (array ) $ relationships );
126
+ if ($ request ->has ('data.relationships ' )) {
127
+ $ this ->updateRecordRelationships ($ record , (array ) $ request -> input ( ' data. relationships' ) );
128
128
}
129
129
130
130
return new JsonApiResponse (new ResourceSerializer ($ record ));
@@ -140,8 +140,7 @@ public function updateAction(Request $request, $record)
140
140
*/
141
141
public function destroyAction (Request $ request , $ record )
142
142
{
143
- $ record = $ this ->findModelInstance ($ record );
144
- $ record ->delete ();
143
+ $ this ->findModelInstance ($ record )->delete ();
145
144
146
145
return new JsonApiResponse (null , Response::HTTP_NO_CONTENT );
147
146
}
@@ -159,7 +158,7 @@ public function destroyAction(Request $request, $record)
159
158
*/
160
159
public function relationshipAction (Request $ request , $ record , $ relation )
161
160
{
162
- abort_if (! array_key_exists ($ relation , $ this ->getModelRelationships ()), Response::HTTP_NOT_FOUND );
161
+ abort_unless ( array_key_exists ($ relation , $ this ->getModelRelationships ()), Response::HTTP_NOT_FOUND );
163
162
164
163
$ record = $ this ->findModelInstance ($ record );
165
164
@@ -180,14 +179,15 @@ public function relationshipAction(Request $request, $record, $relation)
180
179
*/
181
180
public function updateToOneRelationshipAction (Request $ request , $ record , $ relation )
182
181
{
183
- abort_if (! array_key_exists ($ relation , $ this ->getModelRelationships ()), Response::HTTP_NOT_FOUND );
182
+ abort_unless ( array_key_exists ($ relation , $ this ->getModelRelationships ()), Response::HTTP_NOT_FOUND );
184
183
185
184
$ record = $ this ->findModelInstance ($ record );
186
185
$ relation = $ this ->getModelRelationships ()[$ relation ];
187
- $ data = (array ) $ request ->input ('data ' );
188
186
189
- $ record ->{$ relation ->getForeignKey ()} = $ data ['id ' ];
190
- $ record ->save ();
187
+ if ($ relatedRecord = $ relation ->getRelated ()->find ($ request ->input ('data.id ' ))) {
188
+ $ record ->associate ($ relatedRecord );
189
+ $ record ->save ();
190
+ }
191
191
192
192
return new JsonApiResponse (new RelationshipSerializer ($ record , $ relation ));
193
193
}
@@ -206,13 +206,12 @@ public function updateToOneRelationshipAction(Request $request, $record, $relati
206
206
*/
207
207
public function updateToManyRelationshipAction (Request $ request , $ record , $ relation )
208
208
{
209
- abort_if (! array_key_exists ($ relation , $ this ->getModelRelationships ()), Response::HTTP_NOT_FOUND );
209
+ abort_unless ( array_key_exists ($ relation , $ this ->getModelRelationships ()), Response::HTTP_NOT_FOUND );
210
210
211
211
$ record = $ this ->findModelInstance ($ record );
212
- $ relationships = (array ) $ request ->input ('data ' );
213
212
$ items = [];
214
213
215
- foreach ($ relationships as $ item ) {
214
+ foreach (( array ) $ request -> input ( ' data ' ) as $ item ) {
216
215
if (isset ($ item ['attributes ' ])) {
217
216
$ items [$ item ['id ' ]] = $ item ['attributes ' ];
218
217
} else {
0 commit comments