Skip to content

Commit fab1a86

Browse files
committed
fix some bugs
1 parent 91c0387 commit fab1a86

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

src/Adapter/EloquentAdapter.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Illuminate\Database\Eloquent\Collection;
1616
use Illuminate\Database\Eloquent\Model;
1717
use Illuminate\Database\Eloquent\Relations\BelongsTo;
18-
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1918
use InvalidArgumentException;
2019
use Tobyz\JsonApiServer\Schema\Attribute;
2120
use Tobyz\JsonApiServer\Schema\HasMany;
@@ -65,7 +64,7 @@ public function get($query): array
6564

6665
public function count($query): int
6766
{
68-
return $query->count();
67+
return $query->getQuery()->getCountForPagination();
6968
}
7069

7170
public function getId($model): string
@@ -80,20 +79,22 @@ public function getAttribute($model, Attribute $attribute)
8079

8180
public function getHasOne($model, HasOne $relationship, bool $linkage)
8281
{
83-
$relation = $this->getEloquentRelation($model, $relationship);
84-
8582
// If it's a belongs-to relationship and we only need to get the ID,
8683
// then we don't have to actually load the relation because the ID is
8784
// stored in a column directly on the model. We will mock up a related
8885
// model with the value of the ID filled.
89-
if ($linkage && $relation instanceof BelongsTo) {
90-
if ($key = $model->{$relation->getForeignKeyName()}) {
91-
$related = $relation->getRelated();
86+
if ($linkage) {
87+
$relation = $this->getEloquentRelation($model, $relationship);
9288

93-
return $related->newInstance()->forceFill([$related->getKeyName() => $key]);
94-
}
89+
if ($relation instanceof BelongsTo) {
90+
if ($key = $model->{$relation->getForeignKeyName()}) {
91+
$related = $relation->getRelated();
9592

96-
return null;
93+
return $related->newInstance()->forceFill([$related->getKeyName() => $key]);
94+
}
95+
96+
return null;
97+
}
9798
}
9899

99100
return $this->getRelationValue($model, $relationship);

src/Handler/Concerns/IncludesData.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Tobyz\JsonApiServer\JsonApi;
1717
use Tobyz\JsonApiServer\ResourceType;
1818
use Tobyz\JsonApiServer\Schema\Relationship;
19+
use function Tobyz\JsonApiServer\evaluate;
1920
use function Tobyz\JsonApiServer\run_callbacks;
2021

2122
/**
@@ -95,6 +96,7 @@ private function loadRelationshipsAtLevel(array $models, array $relationshipPath
9596
if (
9697
! $field instanceof Relationship
9798
|| (! $field->isLinkage() && ! isset($include[$name]))
99+
|| $field->isVisible() === false
98100
) {
99101
continue;
100102
}
@@ -111,12 +113,12 @@ private function loadRelationshipsAtLevel(array $models, array $relationshipPath
111113

112114
$adapter->load($models, $nextRelationshipPath, $scope, $field->isLinkage());
113115
}
114-
}
115116

116-
if (isset($include[$name]) && is_string($type = $field->getType())) {
117-
$relatedResource = $this->api->getResource($type);
117+
if (isset($include[$name]) && is_string($type = $field->getType())) {
118+
$relatedResource = $this->api->getResource($type);
118119

119-
$this->loadRelationshipsAtLevel($models, $nextRelationshipPath, $relatedResource, $include[$name] ?? [], $request);
120+
$this->loadRelationshipsAtLevel($models, $nextRelationshipPath, $relatedResource, $include[$name] ?? [], $request);
121+
}
120122
}
121123
}
122124
}

src/Handler/Index.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Tobyz\JsonApiServer\Handler;
1313

14+
use Illuminate\Support\Arr;
1415
use JsonApiPhp\JsonApi as Structure;
1516
use JsonApiPhp\JsonApi\Link\LastLink;
1617
use JsonApiPhp\JsonApi\Link\NextLink;
@@ -57,13 +58,11 @@ public function handle(Request $request): Response
5758

5859
$include = $this->getInclude($request);
5960

60-
$this->filter($query, $request);
61+
[$offset, $limit] = $this->paginate($query, $request);
6162
$this->sort($query, $request);
63+
$this->filter($query, $request);
6264

6365
$total = $schema->isCountable() ? $adapter->count($query) : null;
64-
65-
[$offset, $limit] = $this->paginate($query, $request);
66-
6766
$models = $adapter->get($query);
6867

6968
$this->loadRelationships($models, $include, $request);
@@ -107,7 +106,7 @@ private function buildUrl(Request $request, array $overrideParams = []): string
107106
}
108107
}
109108

110-
$queryString = http_build_query($queryParams);
109+
$queryString = Arr::query($queryParams);
111110

112111
return $selfUrl.($queryString ? '?'.$queryString : '');
113112
}

src/Serializer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function addToMap(ResourceType $resource, $model, array $include, bool $
101101
$value = $this->attribute($field, $resource, $model);
102102
} elseif ($field instanceof Schema\Relationship) {
103103
$isIncluded = isset($include[$name]);
104-
$relationshipInclude = $isIncluded ? ($relationshipInclude[$name] ?? []) : null;
104+
$relationshipInclude = $isIncluded ? ($include[$name] ?? []) : null;
105105
$links = $this->relationshipLinks($field, $url);
106106
$meta = $this->meta($field->getMeta(), $model);
107107
$members = array_merge($links, $meta);
@@ -160,7 +160,7 @@ private function toOne(Schema\HasOne $field, array $members, ResourceType $resou
160160
{
161161
$included = $include !== null;
162162

163-
$model = ($included && $getCallback = $field->getGetCallback())
163+
$model = ($getCallback = $field->getGetCallback())
164164
? $getCallback($model, $this->request)
165165
: $resource->getAdapter()->getHasOne($model, $field, ! $included);
166166

@@ -179,7 +179,7 @@ private function toMany(Schema\HasMany $field, array $members, ResourceType $res
179179
{
180180
$included = $include !== null;
181181

182-
$models = ($included && $getCallback = $field->getGetCallback())
182+
$models = ($getCallback = $field->getGetCallback())
183183
? $getCallback($model, $this->request)
184184
: $resource->getAdapter()->getHasMany($model, $field, ! $included);
185185

0 commit comments

Comments
 (0)