如何在adonis js中仅加载现有的预加载数据
我的类别表和餐具表之间有关系
const data = await Category
.query()
.preload('dishes',(builder)=>{
builder.where('user_id',request.input('user_id'));
})
.paginate(1,2);
{"status": 200,"message": "","data": {"meta": {"total": 2,"per_page": 2,"current_page": 1,"last_page": 1,"first_page": 1,"first_page_url": "/?page=1","last_page_url": "/?page=1","next_page_url": null,"previous_page_url": null},"data": [{"id": 1,"name": "Breakfast","status": 1,"created_at": null,"updated_at": null,"dishes": [{"id": 1,"user_id": 14,"category_id": 1,"type": 1,"name": "paneer butter masala","image": null,"description": "paneer","price": 120,"deliverytype": 0,"is_discounted": 0,"created_at": null,"updated_at": null},{"id": 2,"user_id": 14,"category_id": 1,"type": 2,"name": "chiken butter masala","image": null,"description": "chicken","price": 140,"deliverytype": 0,"is_discounted": 0,"created_at": null,"updated_at": null}]},{"id": 2,"name": "Category","status": 1,"created_at": null,"updated_at": null,"dishes": []}]}}
我在Adonis JS工作,
I am working in adonis js I have a relation between my category table and my dishes table but when i preload dishes it loads dishes which have empty relation as well
const data = await Category
.query()
.preload('dishes',(builder)=>{
builder.where('user_id',request.input('user_id'));
})
.paginate(1,2);
{"status": 200,"message": "","data": {"meta": {"total": 2,"per_page": 2,"current_page": 1,"last_page": 1,"first_page": 1,"first_page_url": "/?page=1","last_page_url": "/?page=1","next_page_url": null,"previous_page_url": null},"data": [{"id": 1,"name": "Breakfast","status": 1,"created_at": null,"updated_at": null,"dishes": [{"id": 1,"user_id": 14,"category_id": 1,"type": 1,"name": "paneer butter masala","image": null,"description": "paneer","price": 120,"deliverytype": 0,"is_discounted": 0,"created_at": null,"updated_at": null},{"id": 2,"user_id": 14,"category_id": 1,"type": 2,"name": "chiken butter masala","image": null,"description": "chicken","price": 140,"deliverytype": 0,"is_discounted": 0,"created_at": null,"updated_at": null}]},{"id": 2,"name": "Category","status": 1,"created_at": null,"updated_at": null,"dishes": []}]}}
i want only those category which have relation with dishes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
whereHas
例如。
阅读下面的更多链接
https://docs.adonisjs.com/reference/orm/查询生成器#wherehas
ues the
whereHas
eg.
read more link below
https://docs.adonisjs.com/reference/orm/query-builder#wherehas