11
11
12
12
namespace Tobyz \JsonApiServer \Adapter ;
13
13
14
- use Closure ;
15
14
use Illuminate \Database \Eloquent \Collection ;
16
15
use Illuminate \Database \Eloquent \Model ;
17
16
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
17
+ use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
18
18
use Illuminate \Database \Eloquent \Relations \HasOneThrough ;
19
19
use Illuminate \Database \Eloquent \Relations \MorphOneOrMany ;
20
20
use InvalidArgumentException ;
@@ -116,7 +116,15 @@ public function setAttribute($model, Attribute $attribute, $value): void
116
116
117
117
public function setHasOne ($ model , HasOne $ relationship , $ related ): void
118
118
{
119
- $ this ->getEloquentRelation ($ model , $ relationship )->associate ($ related );
119
+ $ relation = $ this ->getEloquentRelation ($ model , $ relationship );
120
+
121
+ if ($ relation instanceof BelongsTo) {
122
+ if ($ related === null ) {
123
+ $ relation ->dissociate ();
124
+ } else {
125
+ $ relation ->associate ($ related );
126
+ }
127
+ }
120
128
}
121
129
122
130
public function save ($ model ): void
@@ -126,19 +134,19 @@ public function save($model): void
126
134
127
135
public function saveHasMany ($ model , HasMany $ relationship , array $ related ): void
128
136
{
129
- $ this ->getEloquentRelation ($ model , $ relationship )->sync (new Collection ($ related ));
137
+ $ relation = $ this ->getEloquentRelation ($ model , $ relationship );
138
+
139
+ if ($ relation instanceof BelongsToMany) {
140
+ $ relation ->sync (new Collection ($ related ));
141
+ }
130
142
}
131
143
132
144
public function delete ($ model ): void
133
145
{
134
146
// For models that use the SoftDeletes trait, deleting the resource from
135
147
// the API implies permanent deletion. Non-permanent deletion should be
136
148
// achieved by manipulating a resource attribute.
137
- if (method_exists ($ model , 'forceDelete ' )) {
138
- $ model ->forceDelete ();
139
- } else {
140
- $ model ->delete ();
141
- }
149
+ $ model ->forceDelete ();
142
150
}
143
151
144
152
public function filterByIds ($ query , array $ ids ): void
@@ -158,7 +166,9 @@ public function filterByAttribute($query, Attribute $attribute, $value, string $
158
166
public function filterByHasOne ($ query , HasOne $ relationship , array $ ids ): void
159
167
{
160
168
$ relation = $ this ->getEloquentRelation ($ query ->getModel (), $ relationship );
161
- $ column = $ relation instanceof HasOneThrough ? $ relation ->getQualifiedParentKeyName () : $ relation ->getQualifiedForeignKeyName ();
169
+ $ column = $ relation instanceof HasOneThrough
170
+ ? $ relation ->getQualifiedParentKeyName ()
171
+ : $ relation ->getQualifiedForeignKeyName ();
162
172
163
173
$ query ->whereIn ($ column , $ ids );
164
174
}
@@ -199,9 +209,9 @@ public function load(array $models, array $relationships, $scope, bool $linkage)
199
209
$ query = $ relation ->getQuery ();
200
210
201
211
if (is_array ($ scope )) {
202
- // Eloquent doesn't support polymorphic loading constraints,
203
- // so for now we just won't do anything.
204
- // https://github.com/laravel/framework/pull/35190
212
+ // TODO: since https://github.com/laravel/framework/pull/35190
213
+ // was merged, we can now apply loading constraints to
214
+ // polymorphic relationships.
205
215
} else {
206
216
$ scope ($ query );
207
217
}
0 commit comments