|
4 | 4 |
|
5 | 5 | namespace MongoDB\Laravel\Relations;
|
6 | 6 |
|
| 7 | +use Closure; |
7 | 8 | use Illuminate\Database\Eloquent\Collection;
|
8 | 9 | use Illuminate\Database\Eloquent\Model;
|
9 | 10 | use Illuminate\Pagination\LengthAwarePaginator;
|
|
19 | 20 | use function is_array;
|
20 | 21 | use function method_exists;
|
21 | 22 | use function throw_if;
|
| 23 | +use function value; |
22 | 24 |
|
23 | 25 | class EmbedsMany extends EmbedsOneOrMany
|
24 | 26 | {
|
@@ -289,21 +291,22 @@ protected function associateExisting($model)
|
289 | 291 | }
|
290 | 292 |
|
291 | 293 | /**
|
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 |
296 | 299 | *
|
297 | 300 | * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
298 | 301 | */
|
299 |
| - public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
| 302 | + public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null) |
300 | 303 | {
|
301 | 304 | $page = $page ?: Paginator::resolveCurrentPage($pageName);
|
302 |
| - $perPage = $perPage ?: $this->related->getPerPage(); |
303 |
| - |
304 | 305 | $results = $this->getEmbedded();
|
305 | 306 | $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; |
307 | 310 | $start = ($page - 1) * $perPage;
|
308 | 311 |
|
309 | 312 | $sliced = $results->slice(
|
|
0 commit comments