Skip to content

Commit 2ad21fa

Browse files
committed
Repository paginator related
1 parent 99035e5 commit 2ad21fa

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Repositories/Repository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public function resolveShowMeta($request)
504504
*/
505505
public function resolveRelationships($request): array
506506
{
507-
if (is_null($request->get('related'))) {
507+
if (is_null($request->input('related'))) {
508508
return [];
509509
}
510510

@@ -515,9 +515,9 @@ public function resolveRelationships($request): array
515515
if (in_array($relation, static::getRelated())) {
516516
// @todo check if the resource has the relation
517517
/** * @var AbstractPaginator $paginator */
518-
$paginator = $this->resource->{$relation}()->paginate($request->get('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE));
518+
$paginator = $this->resource->{$relation}()->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get();
519519

520-
$withs[$relation] = $paginator->getCollection()->map(fn (Model $item) => [
520+
$withs[$relation] = $paginator->map(fn (Model $item) => [
521521
'attributes' => $item->toArray(),
522522
]);
523523
}

tests/Controllers/RepositoryIndexControllerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ public function test_repository_with_relations()
113113
$this->assertArrayNotHasKey('user', $response->json('data.0.attributes'));
114114
}
115115

116+
public function test_paginated_repository_with_relations()
117+
{
118+
PostRepository::$related = ['user'];
119+
120+
$user = $this->mockUsers(1)->first();
121+
122+
factory(Post::class, 20)->create(['user_id' => $user->id]);
123+
124+
$response = $this->getJson('/restify-api/posts?related=user&page=2')
125+
->assertStatus(200);
126+
127+
$this->assertCount(1, $response->json('data.0.relationships.user'));
128+
$this->assertArrayNotHasKey('user', $response->json('data.0.attributes'));
129+
}
130+
116131
public function test_index_unmergeable_repository_containes_only_explicitly_defined_fields()
117132
{
118133
factory(Post::class)->create();

0 commit comments

Comments
 (0)