好的,这有点令人困惑,但我会尽力而为。
我有这个查询:
const queryBookQuestions = BookUnitQuestion
.query()
.with('user')
.with('book_unit')
.with('book', (builder) => {
builder.where('id', request.params.id)
})
此查询返回一个book_unit
中包含的问题。
我的问题是我需要按book
过滤问题。
我尝试添加:
.with('book', (builder) => {
builder.where('id', request.params.id)
})
但是通过这种方式,我不断收到所有书籍的问题,唯一改变的是book []的json现在是空的:
book: null
book_unit: {id: 4, book_id: 3, unit: 1, sequence: 2, description: "UNIT_01_VOCABU", qt_question: 5, status: false,…}
book_unit_id: 4
correct_answer_description: "teste"
correct_answer_description_id: null
created_at: "2019-11-15 12:13:04"
description: "teste"
id: 3
image_sound: null
option_form: "Texto"
question_form: "Texto"
status: false
type_answer: "Aberta"
updated_at: "2019-11-15 12:13:04"
user: {id: 2, username: "admin"}
user_id: 2
我需要,如果在书中找不到where
,也不要找到问题。
我该怎么做?
这是我的实际模型:
class Book extends Model {
book_units() {
return this.hasMany("App/Models/BookUnit").select(
"id",
"description as unit"
);
}
book_unit_questions() {
return this.manyThrough("App/Models/BookUnit", "book_unit_questions");
}
user () {
return this.belongsTo('App/Models/User')
.select('id', 'username')
}
}
class BookUnit extends Model {
static get table () {
return 'book_unit'
}
book_unit_questions() {
return this.hasMany('App/Models/BookUnitQuestion')
}
user () {
return this.belongsTo('App/Models/User')
.select('id', 'username')
}
}
class BookUnitQuestion extends Model {
static get table () {
return 'book_unit_question'
}
user () {
return this.belongsTo('App/Models/User')
.select('id', 'username')
}
book_unit(){
return this.belongsTo('App/Models/BookUnit')
.select('id','description', 'sequence', 'unit')
}
book(){
return this.belongsTo('App/Models/Book')
}
book_unit_questions() {
return this.belongsTo('App/Models/BookUnit')
}
}