Skip to content

Commit af8c275

Browse files
author
Giacomo Fabbian
committed
[GF] Add docs, use collection for slice operation, fix return type and fix $sliced type
1 parent 4d81b40 commit af8c275

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Collection;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Model as EloquentModel;
8+
use Illuminate\Pagination\AbstractPaginator;
89
use Illuminate\Pagination\LengthAwarePaginator;
910
use Illuminate\Pagination\Paginator;
1011
use MongoDB\BSON\ObjectID;
@@ -264,26 +265,38 @@ protected function associateExisting($model)
264265
return $this->setEmbedded($records);
265266
}
266267

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

277281
$results = $this->getEmbedded();
278-
279-
$total = count($results);
280-
282+
$results = $this->toCollection($results);
283+
$total = $results->count();
281284
$start = ($page - 1) * $perPage;
282-
$sliced = array_slice($results, $start, $perPage);
283285

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

289302
/**

0 commit comments

Comments
 (0)