Commit af8c2755 authored by Giacomo Fabbian's avatar Giacomo Fabbian

[GF] Add docs, use collection for slice operation, fix return type and fix $sliced type

parent 4d81b402
...@@ -5,6 +5,7 @@ namespace Jenssegers\Mongodb\Relations; ...@@ -5,6 +5,7 @@ namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator; use Illuminate\Pagination\Paginator;
use MongoDB\BSON\ObjectID; use MongoDB\BSON\ObjectID;
...@@ -264,26 +265,38 @@ class EmbedsMany extends EmbedsOneOrMany ...@@ -264,26 +265,38 @@ class EmbedsMany extends EmbedsOneOrMany
return $this->setEmbedded($records); return $this->setEmbedded($records);
} }
/** /**
* Get a paginator for the "select" statement. * @param null $perPage
* @param int $perPage * @param array $columns
* @return \Illuminate\Pagination\AbstractPaginator * @param string $pageName
* @param null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/ */
public function paginate($perPage = null) public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{ {
$page = Paginator::resolveCurrentPage(); $page = Paginator::resolveCurrentPage();
$perPage = $perPage ?: $this->related->getPerPage(); $perPage = $perPage ?: $this->related->getPerPage();
$results = $this->getEmbedded(); $results = $this->getEmbedded();
$results = $this->toCollection($results);
$total = count($results); $total = $results->count();
$start = ($page - 1) * $perPage; $start = ($page - 1) * $perPage;
$sliced = array_slice($results, $start, $perPage);
return new LengthAwarePaginator($sliced, $total, $perPage, $page, [ $sliced = $results->slice(
'path' => Paginator::resolveCurrentPath(), $start,
]); $perPage
);
return new LengthAwarePaginator(
$sliced,
$total,
$perPage,
$page,
[
'path' => Paginator::resolveCurrentPath()
]
);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment