wip
This commit is contained in:
parent
b38776365c
commit
25ef9ab56f
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue