Skip to content

Commit 13751a1

Browse files
committed
add links property to JSON pagination responses
1 parent 1e3a8b7 commit 13751a1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Illuminate/Pagination/LengthAwarePaginator.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,36 @@ public function render($view = null, $data = [])
9494
]));
9595
}
9696

97+
/**
98+
* Get the paginator links as a collection (for JSON responses).
99+
*
100+
* @return \Illuminate\Support\Collection
101+
*/
102+
protected function linkCollection()
103+
{
104+
return collect($this->elements())->flatMap(function ($item) {
105+
if (! is_array($item)) {
106+
return [['url' => null, 'label' => '...', 'active' => false]];
107+
}
108+
109+
return collect($item)->map(function ($url, $page) {
110+
return [
111+
'url' => $url,
112+
'label' => $page,
113+
'active' => $this->currentPage() === $page,
114+
];
115+
});
116+
})->prepend([
117+
'url' => $this->previousPageUrl(),
118+
'label' => 'Previous',
119+
'active' => false,
120+
])->push([
121+
'url' => $this->nextPageUrl(),
122+
'label' => 'Next',
123+
'active' => false,
124+
]);
125+
}
126+
97127
/**
98128
* Get the array of elements to pass to the view.
99129
*
@@ -168,6 +198,7 @@ public function toArray()
168198
'from' => $this->firstItem(),
169199
'last_page' => $this->lastPage(),
170200
'last_page_url' => $this->url($this->lastPage()),
201+
'links' => $this->linkCollection(),
171202
'next_page_url' => $this->nextPageUrl(),
172203
'path' => $this->path(),
173204
'per_page' => $this->perPage(),

0 commit comments

Comments
 (0)