Commit f2a84b50 authored by duxet's avatar duxet

Fix pagination of embedded elements

parent 738f224e
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
use MongoId; use MongoId;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Pagination\LengthAwarePaginator;
class EmbedsMany extends EmbedsOneOrMany { class EmbedsMany extends EmbedsOneOrMany {
...@@ -275,17 +275,19 @@ class EmbedsMany extends EmbedsOneOrMany { ...@@ -275,17 +275,19 @@ class EmbedsMany extends EmbedsOneOrMany {
*/ */
public function paginate($perPage = null, $columns = array('*')) public function paginate($perPage = null, $columns = array('*'))
{ {
$page = Paginator::resolveCurrentPage();
$perPage = $perPage ?: $this->related->getPerPage(); $perPage = $perPage ?: $this->related->getPerPage();
$paginator = $this->related->getConnection()->getPaginator();
$results = $this->getEmbedded(); $results = $this->getEmbedded();
$start = ($paginator->getCurrentPage() - 1) * $perPage; $total = count($results);
$start = ($page - 1) * $perPage;
$sliced = array_slice($results, $start, $perPage); $sliced = array_slice($results, $start, $perPage);
return $paginator->make($sliced, count($results), $perPage); return new LengthAwarePaginator($sliced, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath()
]);
} }
/** /**
......
...@@ -746,7 +746,7 @@ class EmbeddedRelationsTest extends TestCase { ...@@ -746,7 +746,7 @@ class EmbeddedRelationsTest extends TestCase {
$results = $user->addresses()->paginate(2); $results = $user->addresses()->paginate(2);
$this->assertEquals(2, $results->count()); $this->assertEquals(2, $results->count());
$this->assertEquals(3, $results->getTotal()); $this->assertEquals(3, $results->total());
} }
} }
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