connecteev's avatar

Pagination on a Laravel Resource?

How to paginate this with Ajax? This is my Posts API which is consumed by my front-end (in Vue/Nuxt). I am trying to implement an infinite scroll but ->paginate(5) (commented out) does not work.


class PostsApiController extends Controller
{
    use MediaUploadingTrait;

    public function index()
    {
        return new PostResource(
            Post::where([
                ['status', '=', 'published'],
            ])
                ->select('id', 'title', 'slug')
                //->take(3)
                //->paginate(5)
                ->get()
        );
    }
}

0 likes
8 replies
connecteev's avatar

@jlrdw Just saw it and it's not what I am looking for.

I am using an API generator called quickadminpanel and for that reason I cannot stray too far from the implementation above - so I need to a way to paginate the PostResource above.

connecteev's avatar

Yeah that video doesn't have anything to do with pagination

connecteev's avatar
connecteev
OP
Best Answer
Level 11

Figured it out: Turns out you cant use get() and paginate() together here. The paginate() works fine without the get()

class PostsApiController extends Controller
{
    use MediaUploadingTrait;

    public function index()
    {
        return new PostResource(
            Post::where([
                ['status', '=', 'published'],
            ])
                ->select('id', 'title', 'slug')
                //->take(3)
                ->paginate(5)
        );
    }
}
jlrdw's avatar

Yeah I had no idea you were using both //->paginate(5) and ->get() together.

1 like

Please or to participate in this conversation.