Skip to content

Commit

Permalink
add links property to JSON pagination responses
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 9, 2020
1 parent 1e3a8b7 commit 13751a1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Illuminate/Pagination/LengthAwarePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ public function render($view = null, $data = [])
]));
}

/**
* Get the paginator links as a collection (for JSON responses).
*
* @return \Illuminate\Support\Collection
*/
protected function linkCollection()
{
return collect($this->elements())->flatMap(function ($item) {
if (! is_array($item)) {
return [['url' => null, 'label' => '...', 'active' => false]];
}

return collect($item)->map(function ($url, $page) {
return [
'url' => $url,
'label' => $page,
'active' => $this->currentPage() === $page,
];
});
})->prepend([
'url' => $this->previousPageUrl(),
'label' => 'Previous',
'active' => false,
])->push([
'url' => $this->nextPageUrl(),
'label' => 'Next',
'active' => false,
]);
}

/**
* Get the array of elements to pass to the view.
*
Expand Down Expand Up @@ -168,6 +198,7 @@ public function toArray()
'from' => $this->firstItem(),
'last_page' => $this->lastPage(),
'last_page_url' => $this->url($this->lastPage()),
'links' => $this->linkCollection(),
'next_page_url' => $this->nextPageUrl(),
'path' => $this->path(),
'per_page' => $this->perPage(),
Expand Down

0 comments on commit 13751a1

Please sign in to comment.