diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index eea7885..37a6ca1 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -46,5 +46,5 @@ interface AdapterInterface public function paginate($query, int $limit, int $offset); - public function load(array $models, array $relationships); + public function load(array $models, array $relationships, \Closure $scope); } diff --git a/src/Adapter/EloquentAdapter.php b/src/Adapter/EloquentAdapter.php index f63042b..f35b361 100644 --- a/src/Adapter/EloquentAdapter.php +++ b/src/Adapter/EloquentAdapter.php @@ -5,6 +5,7 @@ namespace Tobscure\JsonApiServer\Adapter; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\SoftDeletes; use Tobscure\JsonApiServer\Schema\Attribute; use Tobscure\JsonApiServer\Schema\HasMany; @@ -185,9 +186,13 @@ class EloquentAdapter implements AdapterInterface $query->take($limit)->skip($offset); } - public function load(array $models, array $trail) + public function load(array $models, array $trail, \Closure $scope) { - (new Collection($models))->load($this->relationshipTrailToPath($trail)); + (new Collection($models))->load([ + $this->relationshipTrailToPath($trail) => function ($relation) use ($scope) { + $scope($relation->getQuery()); + } + ]); } public function loadIds(array $models, Relationship $relationship) @@ -199,7 +204,7 @@ class EloquentAdapter implements AdapterInterface $property = $this->getRelationshipProperty($relationship); $relation = $models[0]->$property(); - if ($relation instanceof BelongsTo) { + if ($relation instanceof BelongsTo || $relation instanceof BelongsToMany) { return; } diff --git a/src/Handler/Concerns/IncludesData.php b/src/Handler/Concerns/IncludesData.php index ca84673..f37a2c7 100644 --- a/src/Handler/Concerns/IncludesData.php +++ b/src/Handler/Concerns/IncludesData.php @@ -119,7 +119,13 @@ trait IncludesData // TODO: probably need to loop through relationships here ($loader)($models, false); } else { - $adapter->load($models, $relationships); + $scope = function ($query) use ($relationships, $request) { + foreach ($this->api->getResource(end($relationships)->resource)->getSchema()->scopes as $scope) { + $scope($request, $query); + } + }; + + $adapter->load($models, $relationships, $scope); } } } diff --git a/src/Schema/HasMany.php b/src/Schema/HasMany.php index 77536bd..9f97a63 100644 --- a/src/Schema/HasMany.php +++ b/src/Schema/HasMany.php @@ -12,18 +12,4 @@ class HasMany extends Relationship $this->resource = $name; } - - public function includable() - { - $this->includable = true; - - return $this; - } - - public function included() - { - $this->includable(); - - return parent::included(); - } } diff --git a/src/Schema/Relationship.php b/src/Schema/Relationship.php index 21f2c55..b324981 100644 --- a/src/Schema/Relationship.php +++ b/src/Schema/Relationship.php @@ -13,6 +13,7 @@ abstract class Relationship extends Field public $loadable = true; public $loader; public $included = false; + public $includable = true; public $resource; public function __construct(string $name) @@ -78,8 +79,24 @@ abstract class Relationship extends Field return $this; } + public function includable() + { + $this->includable = true; + + return $this; + } + + public function notIncludable() + { + $this->includable = false; + + return $this; + } + public function included() { + $this->includable(); + $this->included = true; return $this;