Skip to content

Commit 25ef9ab

Browse files
committed
wip
1 parent b387763 commit 25ef9ab

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

src/Adapter/AdapterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ public function sortByAttribute($query, Attribute $attribute, string $direction)
4646

4747
public function paginate($query, int $limit, int $offset);
4848

49-
public function load(array $models, array $relationships);
49+
public function load(array $models, array $relationships, \Closure $scope);
5050
}

src/Adapter/EloquentAdapter.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Collection;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
89
use Illuminate\Database\Eloquent\SoftDeletes;
910
use Tobscure\JsonApiServer\Schema\Attribute;
1011
use Tobscure\JsonApiServer\Schema\HasMany;
@@ -185,9 +186,13 @@ public function paginate($query, int $limit, int $offset)
185186
$query->take($limit)->skip($offset);
186187
}
187188

188-
public function load(array $models, array $trail)
189+
public function load(array $models, array $trail, \Closure $scope)
189190
{
190-
(new Collection($models))->load($this->relationshipTrailToPath($trail));
191+
(new Collection($models))->load([
192+
$this->relationshipTrailToPath($trail) => function ($relation) use ($scope) {
193+
$scope($relation->getQuery());
194+
}
195+
]);
191196
}
192197

193198
public function loadIds(array $models, Relationship $relationship)
@@ -199,7 +204,7 @@ public function loadIds(array $models, Relationship $relationship)
199204
$property = $this->getRelationshipProperty($relationship);
200205
$relation = $models[0]->$property();
201206

202-
if ($relation instanceof BelongsTo) {
207+
if ($relation instanceof BelongsTo || $relation instanceof BelongsToMany) {
203208
return;
204209
}
205210

src/Handler/Concerns/IncludesData.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ private function loadRelationships(array $models, array $include, Request $reque
119119
// TODO: probably need to loop through relationships here
120120
($loader)($models, false);
121121
} else {
122-
$adapter->load($models, $relationships);
122+
$scope = function ($query) use ($relationships, $request) {
123+
foreach ($this->api->getResource(end($relationships)->resource)->getSchema()->scopes as $scope) {
124+
$scope($request, $query);
125+
}
126+
};
127+
128+
$adapter->load($models, $relationships, $scope);
123129
}
124130
}
125131
}

src/Schema/HasMany.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,4 @@ public function __construct(string $name)
1212

1313
$this->resource = $name;
1414
}
15-
16-
public function includable()
17-
{
18-
$this->includable = true;
19-
20-
return $this;
21-
}
22-
23-
public function included()
24-
{
25-
$this->includable();
26-
27-
return parent::included();
28-
}
2915
}

src/Schema/Relationship.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract class Relationship extends Field
1313
public $loadable = true;
1414
public $loader;
1515
public $included = false;
16+
public $includable = true;
1617
public $resource;
1718

1819
public function __construct(string $name)
@@ -78,8 +79,24 @@ public function load(Closure $callback)
7879
return $this;
7980
}
8081

82+
public function includable()
83+
{
84+
$this->includable = true;
85+
86+
return $this;
87+
}
88+
89+
public function notIncludable()
90+
{
91+
$this->includable = false;
92+
93+
return $this;
94+
}
95+
8196
public function included()
8297
{
98+
$this->includable();
99+
83100
$this->included = true;
84101

85102
return $this;

0 commit comments

Comments
 (0)