A
A
Artem00712018-07-19 14:17:27
Laravel
Artem0071, 2018-07-19 14:17:27

Why does ResourceCollection work differently?

I want to get a collection of posts using pagination
. I do: $user->posts()->paginate()and everything outputs normally. All posts are in data, there are also links and so on.
I want to wrap it in a PostCollection.
If you do it like this, then everything works:

return new PostCollection(
            $user->posts()->paginate()
        );

But I should do something like this:
return $this->setDataEndRender([
            'posts' => new PostCollection(
                $user->posts()->paginate()
            )
        ]);

Only posts and just an array of post records are displayed (without pagination data)
This function itself (setDataEndRender) does not really represent anything, it just contains response()
But for some reason meta data is lost because of this, and I don’t understand what exactly goes wrong ..
I found a similar error , but it does not say how to solve it ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mShpakov, 2018-07-19
@mShpakov

It's not an error
To get pagination data either do ->toArray()
Or use these methods:
https://laravel.com/docs/5.6/pagination#paginator-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question