diff --git a/src/Adapter/EloquentAdapter.php b/src/Adapter/EloquentAdapter.php index b327b1b..c992f74 100644 --- a/src/Adapter/EloquentAdapter.php +++ b/src/Adapter/EloquentAdapter.php @@ -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,20 +79,22 @@ 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 ($key = $model->{$relation->getForeignKeyName()}) { - $related = $relation->getRelated(); + if ($linkage) { + $relation = $this->getEloquentRelation($model, $relationship); - return $related->newInstance()->forceFill([$related->getKeyName() => $key]); + if ($relation instanceof BelongsTo) { + if ($key = $model->{$relation->getForeignKeyName()}) { + $related = $relation->getRelated(); + + return $related->newInstance()->forceFill([$related->getKeyName() => $key]); + } + + return null; } - - return null; } return $this->getRelationValue($model, $relationship); diff --git a/src/Handler/Concerns/IncludesData.php b/src/Handler/Concerns/IncludesData.php index a516f81..00f73d0 100644 --- a/src/Handler/Concerns/IncludesData.php +++ b/src/Handler/Concerns/IncludesData.php @@ -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,12 +113,12 @@ trait IncludesData $adapter->load($models, $nextRelationshipPath, $scope, $field->isLinkage()); } - } - if (isset($include[$name]) && is_string($type = $field->getType())) { - $relatedResource = $this->api->getResource($type); + if (isset($include[$name]) && is_string($type = $field->getType())) { + $relatedResource = $this->api->getResource($type); - $this->loadRelationshipsAtLevel($models, $nextRelationshipPath, $relatedResource, $include[$name] ?? [], $request); + $this->loadRelationshipsAtLevel($models, $nextRelationshipPath, $relatedResource, $include[$name] ?? [], $request); + } } } } diff --git a/src/Handler/Index.php b/src/Handler/Index.php index 8959f93..3997e8e 100644 --- a/src/Handler/Index.php +++ b/src/Handler/Index.php @@ -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 : ''); } diff --git a/src/Serializer.php b/src/Serializer.php index 6c8776a..150db4c 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -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);