Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add custom pagination information in resource #39600

Merged
merged 2 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -43,10 +43,16 @@ protected function paginationInformation($request)
{
$paginated = $this->resource->resource->toArray();

return [
$default = [
'links' => $this->paginationLinks($paginated),
'meta' => $this->meta($paginated),
];

if (method_exists($this->resource, 'paginationInformation')) {
return $this->resource->paginationInformation($request, $paginated, $default);
}

return $default;
}

/**
Expand Down
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class AnonymousResourceCollectionWithPaginationInformation extends AnonymousResourceCollection
{
public function paginationInformation($request)
{
$paginated = $this->resource->toArray();

return [
'current_page' => $paginated['current_page'],
'per_page' => $paginated['per_page'],
'total' => $paginated['total'],
'total_page' => $paginated['last_page'],
];
}
}
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

use Illuminate\Http\Resources\Json\ResourceCollection;

class PostCollectionResourceWithPaginationInformation extends ResourceCollection
{
public $collects = PostResource::class;

public function toArray($request)
{
return ['data' => $this->collection];
}

public function paginationInformation($request)
{
$paginated = $this->resource->toArray();

return [
'current_page' => $paginated['current_page'],
'per_page' => $paginated['per_page'],
'total' => $paginated['total'],
'total_page' => $paginated['last_page'],
];
}
}
@@ -0,0 +1,28 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

use Illuminate\Http\Resources\Json\JsonResource;

class PostResourceWithAnonymousResourceCollectionWithPaginationInformation extends JsonResource
{
public function toArray($request)
{
return ['id' => $this->id, 'title' => $this->title, 'custom' => true];
}

/**
* Create a new anonymous resource collection.
*
* @param mixed $resource
* @return AnonymousResourceCollectionWithPaginationInformation
*/
public static function collection($resource)
{
return tap(new AnonymousResourceCollectionWithPaginationInformation($resource, static::class), function ($collection) {
if (property_exists(static::class, 'preserveKeys')) {
$collection->preserveKeys = (new static([]))->preserveKeys === true;
}
});
}
}
64 changes: 64 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Expand Up @@ -20,7 +20,9 @@
use Illuminate\Tests\Integration\Http\Fixtures\ObjectResource;
use Illuminate\Tests\Integration\Http\Fixtures\Post;
use Illuminate\Tests\Integration\Http\Fixtures\PostCollectionResource;
use Illuminate\Tests\Integration\Http\Fixtures\PostCollectionResourceWithPaginationInformation;
use Illuminate\Tests\Integration\Http\Fixtures\PostResource;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithAnonymousResourceCollectionWithPaginationInformation;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithExtraData;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalAppendedAttributes;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalData;
Expand Down Expand Up @@ -885,6 +887,68 @@ public function testOriginalOnResponseIsCollectionOfModelWhenCollectionResource(
});
}

public function testCollectionResourceWithPaginationInfomation()
{
$posts = collect([
new Post(['id' => 5, 'title' => 'Test Title']),
]);

Route::get('/', function () use ($posts) {
return new PostCollectionResourceWithPaginationInformation(new LengthAwarePaginator($posts, 10, 1, 1));
});

$response = $this->withoutExceptionHandling()->get(
'/',
['Accept' => 'application/json']
);

$response->assertStatus(200);

$response->assertJson([
'data' => [
[
'id' => 5,
'title' => 'Test Title',
],
],
'current_page' => 1,
'per_page' => 1,
'total_page' => 10,
'total' => 10,
]);
}

public function testResourceWithPaginationInfomation()
{
$posts = collect([
new Post(['id' => 5, 'title' => 'Test Title']),
]);

Route::get('/', function () use ($posts) {
return PostResourceWithAnonymousResourceCollectionWithPaginationInformation::collection(new LengthAwarePaginator($posts, 10, 1, 1));
});

$response = $this->withoutExceptionHandling()->get(
'/',
['Accept' => 'application/json']
);

$response->assertStatus(200);

$response->assertJson([
'data' => [
[
'id' => 5,
'title' => 'Test Title',
],
],
'current_page' => 1,
'per_page' => 1,
'total_page' => 10,
'total' => 10,
]);
}

public function testCollectionResourcesAreCountable()
{
$posts = collect([
Expand Down