Skip to content

Commit b266b75

Browse files
authored
[8.x] Handle cursor paginator when no items are found (#42963)
* handle cursor paginator when no items are found * StyleCI suugested fix
1 parent e630199 commit b266b75

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Illuminate/Pagination/AbstractCursorPaginator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public function previousCursor()
155155
return null;
156156
}
157157

158+
if ($this->items->isEmpty()) {
159+
return null;
160+
}
161+
158162
return $this->getCursorForItem($this->items->first(), false);
159163
}
160164

@@ -170,6 +174,10 @@ public function nextCursor()
170174
return null;
171175
}
172176

177+
if ($this->items->isEmpty()) {
178+
return null;
179+
}
180+
173181
return $this->getCursorForItem($this->items->last(), true);
174182
}
175183

tests/Pagination/CursorPaginatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Pagination\Cursor;
66
use Illuminate\Pagination\CursorPaginator;
7+
use Illuminate\Support\Collection;
78
use PHPUnit\Framework\TestCase;
89

910
class CursorPaginatorTest extends TestCase
@@ -76,6 +77,26 @@ public function testCanTransformPaginatorItems()
7677
$this->assertSame([['id' => 6], ['id' => 7]], $p->items());
7778
}
7879

80+
public function testReturnEmptyCursorWhenItemsAreEmpty()
81+
{
82+
$cursor = new Cursor(['id' => 25], true);
83+
84+
$p = new CursorPaginator(Collection::make(), 25, $cursor, [
85+
'path' => 'http://website.com/test',
86+
'cursorName' => 'cursor',
87+
'parameters' => ['id'],
88+
]);
89+
90+
$this->assertInstanceOf(CursorPaginator::class, $p);
91+
$this->assertSame([
92+
'data' => [],
93+
'path' => 'http://website.com/test',
94+
'per_page' => 25,
95+
'next_page_url' => null,
96+
'prev_page_url' => null,
97+
], $p->toArray());
98+
}
99+
79100
protected function getCursor($params, $isNext = true)
80101
{
81102
return (new Cursor($params, $isNext))->encode();

0 commit comments

Comments
 (0)