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