fix some bugs
This commit is contained in:
parent
91c0387b5b
commit
fab1a86925
|
|
@ -15,7 +15,6 @@ use Closure;
|
|||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use InvalidArgumentException;
|
||||
use Tobyz\JsonApiServer\Schema\Attribute;
|
||||
use Tobyz\JsonApiServer\Schema\HasMany;
|
||||
|
|
@ -65,7 +64,7 @@ class EloquentAdapter implements AdapterInterface
|
|||
|
||||
public function count($query): int
|
||||
{
|
||||
return $query->count();
|
||||
return $query->getQuery()->getCountForPagination();
|
||||
}
|
||||
|
||||
public function getId($model): string
|
||||
|
|
@ -80,13 +79,14 @@ class EloquentAdapter implements AdapterInterface
|
|||
|
||||
public function getHasOne($model, HasOne $relationship, bool $linkage)
|
||||
{
|
||||
$relation = $this->getEloquentRelation($model, $relationship);
|
||||
|
||||
// If it's a belongs-to relationship and we only need to get the ID,
|
||||
// then we don't have to actually load the relation because the ID is
|
||||
// stored in a column directly on the model. We will mock up a related
|
||||
// model with the value of the ID filled.
|
||||
if ($linkage && $relation instanceof BelongsTo) {
|
||||
if ($linkage) {
|
||||
$relation = $this->getEloquentRelation($model, $relationship);
|
||||
|
||||
if ($relation instanceof BelongsTo) {
|
||||
if ($key = $model->{$relation->getForeignKeyName()}) {
|
||||
$related = $relation->getRelated();
|
||||
|
||||
|
|
@ -95,6 +95,7 @@ class EloquentAdapter implements AdapterInterface
|
|||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->getRelationValue($model, $relationship);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ use Tobyz\JsonApiServer\Exception\BadRequestException;
|
|||
use Tobyz\JsonApiServer\JsonApi;
|
||||
use Tobyz\JsonApiServer\ResourceType;
|
||||
use Tobyz\JsonApiServer\Schema\Relationship;
|
||||
use function Tobyz\JsonApiServer\evaluate;
|
||||
use function Tobyz\JsonApiServer\run_callbacks;
|
||||
|
||||
/**
|
||||
|
|
@ -95,6 +96,7 @@ trait IncludesData
|
|||
if (
|
||||
! $field instanceof Relationship
|
||||
|| (! $field->isLinkage() && ! isset($include[$name]))
|
||||
|| $field->isVisible() === false
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
|
@ -111,7 +113,6 @@ trait IncludesData
|
|||
|
||||
$adapter->load($models, $nextRelationshipPath, $scope, $field->isLinkage());
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($include[$name]) && is_string($type = $field->getType())) {
|
||||
$relatedResource = $this->api->getResource($type);
|
||||
|
|
@ -120,4 +121,5 @@ trait IncludesData
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Tobyz\JsonApiServer\Handler;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use JsonApiPhp\JsonApi as Structure;
|
||||
use JsonApiPhp\JsonApi\Link\LastLink;
|
||||
use JsonApiPhp\JsonApi\Link\NextLink;
|
||||
|
|
@ -57,13 +58,11 @@ class Index implements RequestHandlerInterface
|
|||
|
||||
$include = $this->getInclude($request);
|
||||
|
||||
$this->filter($query, $request);
|
||||
[$offset, $limit] = $this->paginate($query, $request);
|
||||
$this->sort($query, $request);
|
||||
$this->filter($query, $request);
|
||||
|
||||
$total = $schema->isCountable() ? $adapter->count($query) : null;
|
||||
|
||||
[$offset, $limit] = $this->paginate($query, $request);
|
||||
|
||||
$models = $adapter->get($query);
|
||||
|
||||
$this->loadRelationships($models, $include, $request);
|
||||
|
|
@ -107,7 +106,7 @@ class Index implements RequestHandlerInterface
|
|||
}
|
||||
}
|
||||
|
||||
$queryString = http_build_query($queryParams);
|
||||
$queryString = Arr::query($queryParams);
|
||||
|
||||
return $selfUrl.($queryString ? '?'.$queryString : '');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,7 +101,7 @@ final class Serializer
|
|||
$value = $this->attribute($field, $resource, $model);
|
||||
} elseif ($field instanceof Schema\Relationship) {
|
||||
$isIncluded = isset($include[$name]);
|
||||
$relationshipInclude = $isIncluded ? ($relationshipInclude[$name] ?? []) : null;
|
||||
$relationshipInclude = $isIncluded ? ($include[$name] ?? []) : null;
|
||||
$links = $this->relationshipLinks($field, $url);
|
||||
$meta = $this->meta($field->getMeta(), $model);
|
||||
$members = array_merge($links, $meta);
|
||||
|
|
@ -160,7 +160,7 @@ final class Serializer
|
|||
{
|
||||
$included = $include !== null;
|
||||
|
||||
$model = ($included && $getCallback = $field->getGetCallback())
|
||||
$model = ($getCallback = $field->getGetCallback())
|
||||
? $getCallback($model, $this->request)
|
||||
: $resource->getAdapter()->getHasOne($model, $field, ! $included);
|
||||
|
||||
|
|
@ -179,7 +179,7 @@ final class Serializer
|
|||
{
|
||||
$included = $include !== null;
|
||||
|
||||
$models = ($included && $getCallback = $field->getGetCallback())
|
||||
$models = ($getCallback = $field->getGetCallback())
|
||||
? $getCallback($model, $this->request)
|
||||
: $resource->getAdapter()->getHasMany($model, $field, ! $included);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue