Skip to content

Lucid: Writing complex 'where' conditions using parentheses #1325

Answered by msrumon
msrumon asked this question in Help
Discussion options

You must be logged in to vote

Never knew I could write functions inside andWhere(). This is the targeted code:

const products = await Product.query()
  .where({
    classification,
    species,
    status: 'approved',
    owned_by: null,
  })
  // .andWhere('title', 'like', `%${q}%`)
  // .orWhere('keywords', 'like', `%${q}%`)
  .andWhere((builder) =>
    builder
      .where('title', 'like', `%${q}%`)
      .orWhere('keywords', 'like', `%${q}%`)
  )
  .orderBy('created_at', 'desc')
  .preload('prices', (builder) => builder.preload('invoices'))
  .preload('seller')
  .debug(true);

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by msrumon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
1 participant