This commit is contained in:
Toby Zerner 2019-08-13 20:21:45 +02:00
parent b38776365c
commit 25ef9ab56f
5 changed files with 33 additions and 19 deletions

View File

@ -46,5 +46,5 @@ interface AdapterInterface
public function paginate($query, int $limit, int $offset); public function paginate($query, int $limit, int $offset);
public function load(array $models, array $relationships); public function load(array $models, array $relationships, \Closure $scope);
} }

View File

@ -5,6 +5,7 @@ namespace Tobscure\JsonApiServer\Adapter;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Tobscure\JsonApiServer\Schema\Attribute; use Tobscure\JsonApiServer\Schema\Attribute;
use Tobscure\JsonApiServer\Schema\HasMany; use Tobscure\JsonApiServer\Schema\HasMany;
@ -185,9 +186,13 @@ class EloquentAdapter implements AdapterInterface
$query->take($limit)->skip($offset); $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) public function loadIds(array $models, Relationship $relationship)
@ -199,7 +204,7 @@ class EloquentAdapter implements AdapterInterface
$property = $this->getRelationshipProperty($relationship); $property = $this->getRelationshipProperty($relationship);
$relation = $models[0]->$property(); $relation = $models[0]->$property();
if ($relation instanceof BelongsTo) { if ($relation instanceof BelongsTo || $relation instanceof BelongsToMany) {
return; return;
} }

View File

@ -119,7 +119,13 @@ trait IncludesData
// TODO: probably need to loop through relationships here // TODO: probably need to loop through relationships here
($loader)($models, false); ($loader)($models, false);
} else { } 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);
} }
} }
} }

View File

@ -12,18 +12,4 @@ class HasMany extends Relationship
$this->resource = $name; $this->resource = $name;
} }
public function includable()
{
$this->includable = true;
return $this;
}
public function included()
{
$this->includable();
return parent::included();
}
} }

View File

@ -13,6 +13,7 @@ abstract class Relationship extends Field
public $loadable = true; public $loadable = true;
public $loader; public $loader;
public $included = false; public $included = false;
public $includable = true;
public $resource; public $resource;
public function __construct(string $name) public function __construct(string $name)
@ -78,8 +79,24 @@ abstract class Relationship extends Field
return $this; return $this;
} }
public function includable()
{
$this->includable = true;
return $this;
}
public function notIncludable()
{
$this->includable = false;
return $this;
}
public function included() public function included()
{ {
$this->includable();
$this->included = true; $this->included = true;
return $this; return $this;