Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
e527c5d6
Commit
e527c5d6
authored
Aug 09, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding paginate to embedsMany, fixes #224
parent
4ee7ac65
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
0 deletions
+34
-0
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+22
-0
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+12
-0
No files found.
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
e527c5d6
...
...
@@ -266,6 +266,28 @@ class EmbedsMany extends EmbedsOneOrMany {
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.
*
...
...
tests/EmbeddedRelationsTest.php
View file @
e527c5d6
...
...
@@ -673,4 +673,16 @@ class EmbeddedRelationsTest extends TestCase {
$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
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment