在 adonisjs lucid 中进行搜索查询

make search query in adonisjs lucid

我有这个表的名为 Apps 的模型 id developer_id title body price size version category created_at updated_at

所以我想按标题搜索查询以在标题中找到搜索到的 %string%

const apps = await Apps.query()
.where(‘title’, params.title)
.fetch()

问题是此代码不搜索相似的字符串,只查找精确的 mach

请记住,Lucid 使用 knex.js 作为查询生成器,您可以使用方法 whereRaw。 所以,如果您使用此代码可能会起作用。

const apps = await Apps.query().whereRaw('title like %?%', [params.title]).fetch()