我如何在 adonis 中使用 whereHas?

How i can use whereHas in adonis?

我正在尝试查询 return 一个 class 的测验,仅当 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')

  }

问题已解决 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();