如何在adonis js中仅加载现有的预加载数据

发布于 2025-01-18 20:38:33 字数 1100 浏览 0 评论 0原文

我的类别表和餐具表之间有关系

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2025-01-25 20:38:33

使用 whereHas

例如。

const data = await Category
    .query()
    .whereHas('dishes', (query) => {
        // filter query

        query.where('user_id',request.input('user_id'))
   }).preload('dishes',(builder)=>{
        // pull the actual preload data

         builder.where('user_id',request.input('user_id'));
   }).paginate(1,2);

阅读下面的更多链接

https://docs.adonisjs.com/reference/orm/查询生成器#wherehas

ues the whereHas

eg.

const data = await Category
    .query()
    .whereHas('dishes', (query) => {
        // filter query

        query.where('user_id',request.input('user_id'))
   }).preload('dishes',(builder)=>{
        // pull the actual preload data

         builder.where('user_id',request.input('user_id'));
   }).paginate(1,2);

read more link below

https://docs.adonisjs.com/reference/orm/query-builder#wherehas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文