diff --git a/CHANGELOG.md b/CHANGELOG.md index a43b9acd7..1e6c2fb30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added +- Support for cursor pagination [#2358](https://github.com/jenssegers/laravel-mongodb/pull/2358) by [@Jeroenwv](https://github.com/Jeroenwv). + ## [3.8.4] - 2021-05-27 ### Fixed diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index f77e87c2d..01c39be23 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -13,6 +13,7 @@ class Builder extends EloquentBuilder /** * The methods that should be returned from query builder. + * * @var array */ protected $passthru = [ @@ -191,7 +192,8 @@ public function raw($expression = null) * TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e * wiil be reverted * Issue in laravel frawework https://github.com/laravel/framework/issues/27791. - * @param array $values + * + * @param array $values * @return array */ protected function addUpdatedAtColumn(array $values) @@ -216,4 +218,27 @@ public function getConnection() { return $this->query->getConnection(); } + + /** + * @inheritdoc + */ + protected function ensureOrderForCursorPagination($shouldReverse = false) + { + if (empty($this->query->orders)) { + $this->enforceOrderBy(); + } + + if ($shouldReverse) { + $this->query->orders = collect($this->query->orders)->map(function ($direction) { + return $direction === 1 ? -1 : 1; + })->toArray(); + } + + return collect($this->query->orders)->map(function ($direction, $column) { + return [ + 'column' => $column, + 'direction' => $direction === 1 ? 'asc' : 'desc', + ]; + })->values(); + } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index cc22df587..b2716e178 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -383,6 +383,29 @@ public function testPaginate(): void $this->assertEquals(1, $results->currentPage()); } + public function testCursorPaginate(): void + { + $results = User::cursorPaginate(2); + $this->assertEquals(2, $results->count()); + $this->assertNotNull($results->first()->title); + $this->assertNotNull($results->nextCursor()); + $this->assertTrue($results->onFirstPage()); + + $results = User::cursorPaginate(2, ['name', 'age']); + $this->assertEquals(2, $results->count()); + $this->assertNull($results->first()->title); + + $results = User::orderBy('age', 'desc')->cursorPaginate(2, ['name', 'age']); + $this->assertEquals(2, $results->count()); + $this->assertEquals(37, $results->first()->age); + $this->assertNull($results->first()->title); + + $results = User::whereNotNull('age')->orderBy('age', 'asc')->cursorPaginate(2, ['name', 'age']); + $this->assertEquals(2, $results->count()); + $this->assertEquals(13, $results->first()->age); + $this->assertNull($results->first()->title); + } + public function testUpdate(): void { $this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));