File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
src/Jenssegers/Mongodb/Relations Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -266,6 +266,28 @@ protected function associateExisting($model)
266266 return $ this ->setEmbedded ($ records );
267267 }
268268
269+ /**
270+ * Get a paginator for the "select" statement.
271+ *
272+ * @param int $perPage
273+ * @param array $columns
274+ * @return \Illuminate\Pagination\Paginator
275+ */
276+ public function paginate ($ perPage = null , $ columns = array ('* ' ))
277+ {
278+ $ perPage = $ perPage ?: $ this ->related ->getPerPage ();
279+
280+ $ paginator = $ this ->related ->getConnection ()->getPaginator ();
281+
282+ $ results = $ this ->getEmbedded ();
283+
284+ $ start = ($ paginator ->getCurrentPage () - 1 ) * $ perPage ;
285+
286+ $ sliced = array_slice ($ results , $ start , $ perPage );
287+
288+ return $ paginator ->make ($ sliced , count ($ results ), $ perPage );
289+ }
290+
269291 /**
270292 * Get the embedded records array.
271293 *
Original file line number Diff line number Diff line change @@ -673,4 +673,16 @@ public function testIncrementEmbedded()
673673 $ this ->assertEquals (6 , $ user ->addresses ()->first ()->visited );
674674 }
675675
676+ public function testPaginateEmbedsMany ()
677+ {
678+ $ user = User::create (array ('name ' => 'John Doe ' ));
679+ $ user ->addresses ()->save (new Address (array ('city ' => 'New York ' )));
680+ $ user ->addresses ()->save (new Address (array ('city ' => 'Paris ' )));
681+ $ user ->addresses ()->save (new Address (array ('city ' => 'Brussels ' )));
682+
683+ $ results = $ user ->addresses ()->paginate (2 );
684+ $ this ->assertEquals (2 , $ results ->count ());
685+ $ this ->assertEquals (3 , $ results ->getTotal ());
686+ }
687+
676688}
You can’t perform that action at this time.
0 commit comments