【问题标题】:How i can use whereHas in adonis?我如何在阿多尼斯中使用 whereHas ?
【发布时间】:2023-03-25 20:36:02
【问题描述】:

我正在尝试进行查询,仅当 execution_back_status 等于 "Enviado Perguntas" 时才返回一个班级的测验。

但我收到的记录与 "Enviado Perguntas" 不同

我的尝试:

let quizAbertos = await Class.query()

    .with('quizzes')

    .whereHas('quizzes', builder => {

      builder.where('student_id', idEstudante)

      builder.where('execution_back_status', 'Enviado Perguntas')

     })

    .fetch()

    quizAbertos = quizAbertos.toJSON()

    console.log(JSON.stringify(quizAbertos))

我的console.log()

[
  {
    "id": 1,
    "code": "ING-NOT-2020",
    "description": "Inglês Noturno 2020",
    "start_date": "2020-01-06T03:00:00.000Z",
    "end_date": "2020-03-01T03:00:00.000Z",
    "period": "Noturno",
    "language": "Inglês",
    "status": false,
    "user_id": 9,
    "created_at": "2020-01-06 09:46:30",
    "updated_at": "2020-02-06 08:10:33",
    "language_substring": "US",
    "quizzes": [
      {
        "id": 819,
        "sequence": null,
        "student_id": 1,
        "class_id": 1,
        "book_id": 1,
        "book_unit_id": 1,
        "quiz_id": 828,
        "percentage_correct": 40,
        "review": false,
        "execution_back_status": "Reprovado",
        "user_id": null,
        "created_at": "2020-02-06 10:45:25",
        "updated_at": "2020-02-06 11:16:23"
      },
      {
        "id": 820,
        "sequence": null,
        "student_id": 1,
        "class_id": 1,
        "book_id": 1,
        "book_unit_id": 1,
        "quiz_id": 829,
        "percentage_correct": null,
        "review": false,
        "execution_back_status": "Enviado Perguntas",
        "user_id": null,
        "created_at": "2020-02-06 11:16:52",
        "updated_at": "2020-02-06 11:16:52"
      }
    ]
  }
]

如您所见,我的结果为“execution_back_status”:Reprovado,我只需要值为“Enviado Perguntas”

为什么会这样?

Class 模型中我有:

quizzes () {

   return this.hasMany('App/Models/StudentQuizHistorics')

  }

【问题讨论】:

    标签: node.js adonis.js


    【解决方案1】:

    问题已在forum.adonisjs.com 上解决

    使用 with() 过滤帖子,使用 whereHas 过滤用户。喜欢:

    // Testing code
    var result = User.query()
        .with("posts", builder => {
          // Filter on the posts array
          builder.where("cond1", "2");
          builder.where("cond2", "3");
        })
        .whereHas("posts", builder => {
          // Filter user
          builder.where("cond1", "1");
          builder.where("cond2", "3");
        })
        .fetch();
    

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多