Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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'],
];
}
}
Original file line number Diff line number Diff line change
@@ -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'],
];
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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