【问题标题】:Adonis js pagination with dynamic query building具有动态查询构建的 Adonis js 分页
【发布时间】:2019-09-16 06:59:18
【问题描述】:

我正在构建这样的查询

    let searchData = request.all()

    let products =  Product.query().where('product_type', searchData.type).with('singleImage').paginate(1, 20)

    if(parseInt(searchData.minPrice) > 0 && parseInt(searchData.maxPrice) > 0) {
        products.where("lowerPrice", ">=", searchData.minPrice).where("upperPrice", "<=", searchData.maxPrice)
    }

    // more conditions and query goes here 

因为.paginate(1, 20) 它不起作用,如果我删除分页它就起作用。

【问题讨论】:

  • 你有什么解决办法??
  • 是的。分页和获取是一回事。所以我们必须在最后使用 await products.paginate(page, 1) 。 paginate 将作为 fetch 工作。
  • 非常感谢您告诉我

标签: adonis.js


【解决方案1】:

设置所有条件后,您需要执行 paginate 或 fetch。例如:

let searchData = request.all()

let products = Product.query()
          .where('product_type',searchData.type)
          .with('singleImage')
// Remove pagination from here

if(parseInt(searchData.minPrice) > 0 && parseInt(searchData.maxPrice) > 0) 
{
 products.where("lowerPrice", ">=", searchData.minPrice)
         .where("upperPrice", "<=", searchData.maxPrice)
}

// Put all your conditions 


// Finally execute paginate with await or yield it depends of adonis version (await or yield)

products = await products.paginate(1,20)

【讨论】:

    猜你喜欢
    • 2019-11-24
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2012-12-21
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多