Skip to content

Commit c0966a9

Browse files
authored
Merge pull request #1959 from Giacomo92/pr_1145
[Updated PR#1145] Fix paginate in EmbedsMany
2 parents 89fe91f + 70c7263 commit c0966a9

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,36 @@ protected function associateExisting($model)
265265
}
266266

267267
/**
268-
* Get a paginator for the "select" statement.
269-
* @param int $perPage
270-
* @return \Illuminate\Pagination\AbstractPaginator
268+
* @param null $perPage
269+
* @param array $columns
270+
* @param string $pageName
271+
* @param null $page
272+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
271273
*/
272-
public function paginate($perPage = null)
274+
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
273275
{
274-
$page = Paginator::resolveCurrentPage();
276+
$page = $page ?: Paginator::resolveCurrentPage($pageName);
275277
$perPage = $perPage ?: $this->related->getPerPage();
276278

277279
$results = $this->getEmbedded();
278-
279-
$total = count($results);
280-
280+
$results = $this->toCollection($results);
281+
$total = $results->count();
281282
$start = ($page - 1) * $perPage;
282-
$sliced = array_slice($results, $start, $perPage);
283283

284-
return new LengthAwarePaginator($sliced, $total, $perPage, $page, [
285-
'path' => Paginator::resolveCurrentPath(),
286-
]);
284+
$sliced = $results->slice(
285+
$start,
286+
$perPage
287+
);
288+
289+
return new LengthAwarePaginator(
290+
$sliced,
291+
$total,
292+
$perPage,
293+
$page,
294+
[
295+
'path' => Paginator::resolveCurrentPath()
296+
]
297+
);
287298
}
288299

289300
/**

0 commit comments

Comments
 (0)