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

[6.x] Allow developer to preserve query parameters on paginated Api resources #30745

Merged
merged 6 commits into from
Dec 5, 2019
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
42 changes: 39 additions & 3 deletions src/Illuminate/Http/Resources/Json/ResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class ResourceCollection extends JsonResource implements Countable, IteratorAggr
*/
public $collection;

/**
* Determines whether to preserve all query parameters when generating the navigation links.
*
* @var bool
*/
protected $queryParameters;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe shouldPreserveQueryParamters would be better and expressive.


/**
* Create a new resource instance.
*
Expand All @@ -38,6 +45,18 @@ public function __construct($resource)
$this->resource = $this->collectResource($resource);
}

/**
* Preserve all query parameters when generating the navigation links.
*
* @return $this
*/
public function preserveQueryParameters()
{
$this->queryParameters = true;

return $this;
}

/**
* Return the count of items in the resource collection.
*
Expand Down Expand Up @@ -67,8 +86,25 @@ public function toArray($request)
*/
public function toResponse($request)
{
return $this->resource instanceof AbstractPaginator
? (new PaginatedResourceResponse($this))->toResponse($request)
: parent::toResponse($request);
if ($this->resource instanceof AbstractPaginator) {
return $this->preparePaginatedResponse($request);
}

return parent::toResponse($request);
}

/**
* Create a paginate-aware HTTP response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
protected function preparePaginatedResponse($request)
{
if ($this->queryParameters) {
$this->resource->appends($request->query());
}

return (new PaginatedResourceResponse($this))->toResponse($request);
}
}
42 changes: 42 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,48 @@ public function testPaginatorsReceiveLinks()
]);
}

public function testPaginatorResourceCanPreserveQueryParameters()
{
Route::get('/', function () {
$collection = collect([new Post(['id' => 2, 'title' => 'Laravel Nova'])]);
$paginator = new LengthAwarePaginator(
$collection, 3, 1, 2
);

return PostCollectionResource::make($paginator)->preserveQueryParameters();
});

$response = $this->withoutExceptionHandling()->get(
'/?framework=laravel&author=Otwell&page=2', ['Accept' => 'application/json']
);

$response->assertStatus(200);

$response->assertJson([
'data' => [
[
'id' => 2,
'title' => 'Laravel Nova',
],
],
'links' => [
'first' => '/?framework=laravel&author=Otwell&page=1',
'last' => '/?framework=laravel&author=Otwell&page=3',
'prev' => '/?framework=laravel&author=Otwell&page=1',
'next' => '/?framework=laravel&author=Otwell&page=3',
],
'meta' => [
'current_page' => 2,
'from' => 2,
'last_page' => 3,
'path' => '/',
'per_page' => 1,
'to' => 2,
'total' => 3,
],
]);
}

public function testToJsonMayBeLeftOffOfCollection()
{
Route::get('/', function () {
Expand Down