diff --git a/src/Relations/EmbedsMany.php b/src/Relations/EmbedsMany.php index 2d68af70b..a9521fb45 100644 --- a/src/Relations/EmbedsMany.php +++ b/src/Relations/EmbedsMany.php @@ -4,6 +4,7 @@ namespace MongoDB\Laravel\Relations; +use Closure; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Pagination\LengthAwarePaginator; @@ -19,6 +20,7 @@ use function is_array; use function method_exists; use function throw_if; +use function value; class EmbedsMany extends EmbedsOneOrMany { @@ -289,21 +291,22 @@ protected function associateExisting($model) } /** - * @param int|null $perPage - * @param array $columns - * @param string $pageName - * @param int|null $page + * @param int|Closure $perPage + * @param array|string $columns + * @param string $pageName + * @param int|null $page + * @param Closure|int|null $total * * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ - public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) + public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null) { $page = $page ?: Paginator::resolveCurrentPage($pageName); - $perPage = $perPage ?: $this->related->getPerPage(); - $results = $this->getEmbedded(); $results = $this->toCollection($results); - $total = $results->count(); + $total = value($total) ?? $results->count(); + $perPage = $perPage ?: $this->related->getPerPage(); + $perPage = $perPage instanceof Closure ? $perPage($total) : $perPage; $start = ($page - 1) * $perPage; $sliced = $results->slice(