Skip to content

Commit d303169

Browse files
committed
Paginator total override
laravel/framework#46410
1 parent e79b0ae commit d303169

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [4.5.0] - upcoming
4+
## [4.6.0] - upcoming
55

6-
* Add GridFS integration for Laravel File Storage by @GromNaN in [#2984](https://github.com/mongodb/laravel-mongodb/pull/2985)
6+
* Add support for Closure for Embed pagination @GromNaN in [#3027](https://github.com/mongodb/laravel-mongodb/pull/3027)
7+
8+
## [4.5.0] - 2024-06-20
9+
10+
* Add GridFS integration for Laravel File Storage by @GromNaN in [#2984](https://github.com/mongodb/laravel-mongodb/pull/2984)
711

812
## [4.4.0] - 2024-05-31
913

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;
@@ -19,6 +20,7 @@
1920
use function is_array;
2021
use function method_exists;
2122
use function throw_if;
23+
use function value;
2224

2325
class EmbedsMany extends EmbedsOneOrMany
2426
{
@@ -289,21 +291,22 @@ protected function associateExisting($model)
289291
}
290292

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

309312
$sliced = $results->slice(

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)