Skip to content

Commit

Permalink
[8.x] Add custom pagination information in resource (#39600)
Browse files Browse the repository at this point in the history
* add custom pagination information in resource

* Update PaginatedResourceResponse.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
chuoke and taylorotwell committed Nov 22, 2021
1 parent e1cae7e commit e9f05f1
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
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

0 comments on commit e9f05f1

Please sign in to comment.