How on AdonisJs I can paginate relations?

#1

Hi everyone. How I can paginate relation model data on AdonisJs?
Is there any common way developers do?
I know that I can prepare response as

total: "20"
page: 1
category = await Category.query()
    .with('posts', builder => {
         builder.select('id', 'title', 'description', 'h1', 'text') // .paginate() // as example
     })
      .where('slug', params.slug)
      .firstOrFail();

manually, but maybe there is some method, as on Laravel?

1 Like

#2

Oh, I found solution! Here is an example:

category = await Category.findBy('slug', params.slug)
posts = await category.posts().paginate(1, 10)
1 Like

#3

Yes, sometimes it helps to read the official documentation :slight_smile:

0 Likes

#4

The documentation is a bit confusing as sometimes I go to 4.0 but using 4.1.

0 Likes