Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination in readRelatedResource #22

Closed
stfnndrsn opened this issue Sep 19, 2016 · 4 comments
Closed

Pagination in readRelatedResource #22

stfnndrsn opened this issue Sep 19, 2016 · 4 comments
Milestone

Comments

@stfnndrsn
Copy link
Contributor

stfnndrsn commented Sep 19, 2016

I can't figure out an way of doing pagination of a related resource.

EloquentController#readRelatedResource

        return $this
            ->reply()
            ->content($model->{$key});
@stfnndrsn
Copy link
Contributor Author

I would gladly help out and update the functionality

@lindyhopchris
Copy link
Member

Hi! Yes, I need to do some work on the related resources stuff so it is all taken care off out of the box, but haven't got round to that yet.

In the meantime the following should work if you're on the 0.5.x-dev branch:

$paging = app(CloudCreativity\JsonApi\Contracts\Pagination\PaginatorInterface::class);
$page = app(CloudCreativity\LaravelJsonApi\Pagination\Page::class);

/** If the client has requested a page... */
if ($paging->getCurrentPage()) {
    $data = $model->{$key}()->paginate($paging->getPerPage());
} /** Client has not request a page */
else {
    $data = $model->{$key};
}

$page->setData($data);

return $this->reply()->content($page);

Let me know how you get on and obviously I can help more if you're still stuck. I'll probably be able to land proper related resource handling in 0.6 as I'm going to be tagging 0.5 very soon.

@lindyhopchris
Copy link
Member

Actually, slight correction. The page object will only accept a Laravel paginator in its setData method, so it should be this:

$paging = app(CloudCreativity\JsonApi\Contracts\Pagination\PaginatorInterface::class);
$page = app(CloudCreativity\LaravelJsonApi\Pagination\Page::class);

/** If the client has requested a page... */
if ($paging->getCurrentPage()) {
    $data = $model->{$key}()->paginate($paging->getPerPage());
    $data = $page->setData($data);
} /** Client has not request a page */
else {
    $data = $model->{$key};
}

return $this->reply()->content($data);

@lindyhopchris lindyhopchris added this to the 0.6.0 milestone Oct 18, 2016
@lindyhopchris lindyhopchris mentioned this issue May 18, 2017
15 tasks
@lindyhopchris
Copy link
Member

On the 1.0 dev branch you can now paginate related resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants