Skip to content

Commit

Permalink
Paginator total override
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jul 5, 2024
1 parent e79b0ae commit fd67dac
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Relations/EmbedsMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MongoDB\Laravel\Relations;

use Closure;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
Expand All @@ -19,6 +20,7 @@
use function is_array;
use function method_exists;
use function throw_if;
use function value;

class EmbedsMany extends EmbedsOneOrMany
{
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit fd67dac

Please sign in to comment.