How to filter relationships in Donis?

Asked

Viewed 330 times

-1

I pass this query:

const filter = request.input('filter')
const queryBookQuestions =  BookUnitQuestion
                                    .query()
                                    .with('book_unit')
                                    .with('user')
                                    .with('book', (builder) => {
                                        builder.where('id', request.params.id)
                                    })

In this query I return all the books of a book unit with the object book_unit related to it, the user and bring only the questions of the book that receive as parameter (book.params.id)

Now I need to filter the questions by units. I have this filter that works correctly looking for the description of the question:

if(filter){
            if(filter.search("description") !== -1){
                let description = filter.match(/(?<=description~contains~').*?(?=')/)
                queryBookQuestions.where('description', 'ilike', '%'+description[0]+'%')
            }

This filter works well because I believe it is applied directly in the model BookUnitQuestion. But now, I need to filter the Unit, which is a relationship, that is, I need to return the questions that have only in the unit that I filter. I tried something like:

if(filter.search("unit") !== -1){
                let unit = request.input('filter').match(/(?<=unit~contains~').*?(?=')/)

                queryBookQuestions.where('book_unit.unit', unit[0])}

        }

But this way I get:

select Count(*) as "total" from "book_unit_question" Where "book_unit"." Unit" = $1 - Missing FROM-clause entry for table "book_unit"

1 answer

0

It seems a matter of the moment of processing the arguments. Try changing the Where to whereRaw. You must solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.