Skip to content

Commit cf9fcb9

Browse files
committed
Paginator total override
laravel/framework#46410
1 parent f6ee9cc commit cf9fcb9

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [4.6.0] - upcoming
55

66
* Add `DocumentTrait` to use any 3rd party model with MongoDB @GromNaN in [#2580](https://github.com/mongodb/laravel-mongodb/pull/2580)
7+
* * Add support for Closure for Embed pagination @GromNaN in [#3027](https://github.com/mongodb/laravel-mongodb/pull/3027)
78

89
## [4.5.0] - 2024-06-20
910

Diff for: src/Relations/EmbedsMany.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MongoDB\Laravel\Relations;
66

7+
use Closure;
78
use Illuminate\Database\Eloquent\Collection;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Pagination\LengthAwarePaginator;
@@ -18,6 +19,7 @@
1819
use function is_array;
1920
use function method_exists;
2021
use function throw_if;
22+
use function value;
2123

2224
class EmbedsMany extends EmbedsOneOrMany
2325
{
@@ -288,21 +290,22 @@ protected function associateExisting($model)
288290
}
289291

290292
/**
291-
* @param int|null $perPage
292-
* @param array $columns
293-
* @param string $pageName
294-
* @param int|null $page
293+
* @param int|Closure $perPage
294+
* @param array|string $columns
295+
* @param string $pageName
296+
* @param int|null $page
297+
* @param Closure|int|null $total
295298
*
296299
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
297300
*/
298-
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
301+
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
299302
{
300303
$page = $page ?: Paginator::resolveCurrentPage($pageName);
301-
$perPage = $perPage ?: $this->related->getPerPage();
302-
303304
$results = $this->getEmbedded();
304305
$results = $this->toCollection($results);
305-
$total = $results->count();
306+
$total = value($total) ?? $results->count();
307+
$perPage = $perPage ?: $this->related->getPerPage();
308+
$perPage = $perPage instanceof Closure ? $perPage($total) : $perPage;
306309
$start = ($page - 1) * $perPage;
307310

308311
$sliced = $results->slice(

Diff for: tests/EmbeddedRelationsTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,12 @@ public function testPaginateEmbedsMany()
925925
$results = $user->addresses()->paginate(2);
926926
$this->assertEquals(2, $results->count());
927927
$this->assertEquals(3, $results->total());
928+
929+
// With Closures
930+
$results = $user->addresses()->paginate(fn () => 3, page: 1, total: fn () => 5);
931+
$this->assertEquals(3, $results->count());
932+
$this->assertEquals(5, $results->total());
933+
$this->assertEquals(3, $results->perPage());
928934
}
929935

930936
public function testGetQueueableRelationsEmbedsMany()

0 commit comments

Comments
 (0)