Skip to content

Commit

Permalink
Merge branch 'append-query-parameters' of https://github.com/deleugpn…
Browse files Browse the repository at this point in the history
…/framework into deleugpn-append-query-parameters
  • Loading branch information
taylorotwell committed Dec 5, 2019
2 parents 27efba7 + 43cf1db commit f9ae57f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
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;

/**
* 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

0 comments on commit f9ae57f

Please sign in to comment.