Skip to content

Commit fd67dac

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

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

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;
@@ -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(

0 commit comments

Comments
 (0)