How to make pagination on `query builder` work when assign to variable?


#1

pagination on query builder work with chaining function

const posts =  await Database.table('posts as p').select('p.id','p.post_title','p.created_at', 'p.updated_at')
.paginate(1, 3);
return posts;

How to make pagination on query builder work when assign to variable?
the below query only return all data

const posts =  Database.table('posts as p');
posts.select('p.id','p.post_title','p.created_at', 'p.updated_at');
posts.paginate(1, 3);
return posts;

#2

you tried use?

posts.where('field', '=', request.input('field'))

Where clauses


#3

the problem is not post.where, I want to make pagination work when I assign query to posts variable.


#4

Your query formation is fine, you need to useawait when executing the query

await posts.paginate(1)

#5

thanks for reply, pagination still not work :frowning:

const posts =  Database.table('posts');
await posts.paginate(1,2);
return posts;

the result is

[{
"id": 1,
"post_title": "Ahi tekosir famrij lamdapup uc evotow hefamwon hibfupida"
}]

only work when using this query :

 const posts =  await Database.table('posts').paginate(1,2);
 return posts;

result :

{
"total": 212,
"perPage": 2,
"page": 1,
"lastPage": 106,
"data": [{
"id": 1,
"post_title": "Ahi tekosir famrij lamdapup uc evotow hefamwon hibfupida",
}]
}

package.json

"@adonisjs/ace": "^5.0.4",
    "@adonisjs/auth": "^3.0.6",
    "@adonisjs/bodyparser": "^2.0.4",
    "@adonisjs/cors": "^1.0.6",
    "@adonisjs/fold": "^4.0.9",
    "@adonisjs/framework": "^5.0.8",
    "@adonisjs/ignitor": "^2.0.7",
    "@adonisjs/lucid": "^6.0.1",
    "@adonisjs/mail": "^3.0.8",
    "@adonisjs/session": "^1.0.26",
    "@adonisjs/shield": "^1.0.7",
    "@adonisjs/validator": "^5.0.3",

is this bug?


#6

You have to assign the value of posts.paginate to a variable.

const posts = Database.table('posts'); 

const output = await posts.paginate(1,2); 

return output

#7

my fault, thanks virk :smile: