Commit e527c5d6 authored by Jens Segers's avatar Jens Segers

Adding paginate to embedsMany, fixes #224

parent 4ee7ac65
...@@ -266,6 +266,28 @@ class EmbedsMany extends EmbedsOneOrMany { ...@@ -266,6 +266,28 @@ class EmbedsMany extends EmbedsOneOrMany {
return $this->setEmbedded($records); return $this->setEmbedded($records);
} }
/**
* Get a paginator for the "select" statement.
*
* @param int $perPage
* @param array $columns
* @return \Illuminate\Pagination\Paginator
*/
public function paginate($perPage = null, $columns = array('*'))
{
$perPage = $perPage ?: $this->related->getPerPage();
$paginator = $this->related->getConnection()->getPaginator();
$results = $this->getEmbedded();
$start = ($paginator->getCurrentPage() - 1) * $perPage;
$sliced = array_slice($results, $start, $perPage);
return $paginator->make($sliced, count($results), $perPage);
}
/** /**
* Get the embedded records array. * Get the embedded records array.
* *
......
...@@ -673,4 +673,16 @@ class EmbeddedRelationsTest extends TestCase { ...@@ -673,4 +673,16 @@ class EmbeddedRelationsTest extends TestCase {
$this->assertEquals(6, $user->addresses()->first()->visited); $this->assertEquals(6, $user->addresses()->first()->visited);
} }
public function testPaginateEmbedsMany()
{
$user = User::create(array('name' => 'John Doe'));
$user->addresses()->save(new Address(array('city' => 'New York')));
$user->addresses()->save(new Address(array('city' => 'Paris')));
$user->addresses()->save(new Address(array('city' => 'Brussels')));
$results = $user->addresses()->paginate(2);
$this->assertEquals(2, $results->count());
$this->assertEquals(3, $results->getTotal());
}
} }
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