-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Add custom pagination information in resource (#39600)
* add custom pagination information in resource * Update PaginatedResourceResponse.php Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
e1cae7e
commit e9f05f1
Showing
5 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/Integration/Http/Fixtures/AnonymousResourceCollectionWithPaginationInformation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Integration/Http/Fixtures/PostCollectionResourceWithPaginationInformation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...on/Http/Fixtures/PostResourceWithAnonymousResourceCollectionWithPaginationInformation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters