Skip to content

Commit b57b6c3

Browse files
committed
Tighten up EloquentAdapter
1 parent 71d5114 commit b57b6c3

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/Adapter/EloquentAdapter.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Tobyz\JsonApiServer\Adapter;
1313

14-
use Closure;
1514
use Illuminate\Database\Eloquent\Collection;
1615
use Illuminate\Database\Eloquent\Model;
1716
use Illuminate\Database\Eloquent\Relations\BelongsTo;
17+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1818
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
1919
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
2020
use InvalidArgumentException;
@@ -116,7 +116,15 @@ public function setAttribute($model, Attribute $attribute, $value): void
116116

117117
public function setHasOne($model, HasOne $relationship, $related): void
118118
{
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+
}
120128
}
121129

122130
public function save($model): void
@@ -126,19 +134,19 @@ public function save($model): void
126134

127135
public function saveHasMany($model, HasMany $relationship, array $related): void
128136
{
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+
}
130142
}
131143

132144
public function delete($model): void
133145
{
134146
// For models that use the SoftDeletes trait, deleting the resource from
135147
// the API implies permanent deletion. Non-permanent deletion should be
136148
// achieved by manipulating a resource attribute.
137-
if (method_exists($model, 'forceDelete')) {
138-
$model->forceDelete();
139-
} else {
140-
$model->delete();
141-
}
149+
$model->forceDelete();
142150
}
143151

144152
public function filterByIds($query, array $ids): void
@@ -158,7 +166,9 @@ public function filterByAttribute($query, Attribute $attribute, $value, string $
158166
public function filterByHasOne($query, HasOne $relationship, array $ids): void
159167
{
160168
$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();
162172

163173
$query->whereIn($column, $ids);
164174
}
@@ -199,9 +209,9 @@ public function load(array $models, array $relationships, $scope, bool $linkage)
199209
$query = $relation->getQuery();
200210

201211
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.
205215
} else {
206216
$scope($query);
207217
}

0 commit comments

Comments
 (0)