Skip to content

Commit f2a84b5

Browse files
committed
Fix pagination of embedded elements
1 parent 738f224 commit f2a84b5

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use MongoId;
44
use Illuminate\Database\Eloquent\Model;
5-
use Illuminate\Database\Eloquent\Builder;
6-
use Illuminate\Database\Eloquent\Relations\Relation;
5+
use Illuminate\Pagination\Paginator;
6+
use Illuminate\Pagination\LengthAwarePaginator;
77

88
class EmbedsMany extends EmbedsOneOrMany {
99

@@ -275,17 +275,19 @@ protected function associateExisting($model)
275275
*/
276276
public function paginate($perPage = null, $columns = array('*'))
277277
{
278+
$page = Paginator::resolveCurrentPage();
278279
$perPage = $perPage ?: $this->related->getPerPage();
279280

280-
$paginator = $this->related->getConnection()->getPaginator();
281-
282281
$results = $this->getEmbedded();
283282

284-
$start = ($paginator->getCurrentPage() - 1) * $perPage;
283+
$total = count($results);
285284

285+
$start = ($page - 1) * $perPage;
286286
$sliced = array_slice($results, $start, $perPage);
287287

288-
return $paginator->make($sliced, count($results), $perPage);
288+
return new LengthAwarePaginator($sliced, $total, $perPage, $page, [
289+
'path' => Paginator::resolveCurrentPath()
290+
]);
289291
}
290292

291293
/**

tests/EmbeddedRelationsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ public function testPaginateEmbedsMany()
746746

747747
$results = $user->addresses()->paginate(2);
748748
$this->assertEquals(2, $results->count());
749-
$this->assertEquals(3, $results->getTotal());
749+
$this->assertEquals(3, $results->total());
750750
}
751751

752752
}

0 commit comments

Comments
 (0)