我试图找到一种使用AdonisJs执行搜索查询的方法,但我无法找到用Lucid进行搜索查询的任何方法。
我目前正在使用这个工具,但这显然不是进行搜索查询的正确方法:
let posts = yield Post.query().with('category').where({
title: request.input('term')
}).forPage(1, 10).fetch()如何使用adonis.js框架直接执行postgres查询?
SELECT id FROM posts WHERE content LIKE '%searchterm%' OR WHERE tags LIKE '%searchterm%' OR WHERE title LIKE '%searchterm%'发布于 2017-05-26 16:01:14
找到了用Database.schema.raw(execute queries here)在Adonis中直接执行SQL查询的解决方案,因此:
const postQuery = yield Database.schema.raw('SELECT * FROM posts');
const posts = postQuery.rows
console.log(posts);编辑1
若要使用Lucid执行搜索查询,请执行以下操作:
const term = request.input('term');
yield Database.select('*').from('posts').where('title', 'LIKE', '%'+term+'%')
console.log(posts);编辑2
更好的原始查询:
yield Database.select('*').from('posts').whereRaw('title @@ :term OR description @@ :term', {term: '%'+term+'%'})发布于 2019-03-05 07:25:33
假设您有一个名为Post.You的模型,则可以使用以下查询。
const term = request.input('term');
const posts = await Post.query().where('title', 'LIKE', '%'+term+'%').fetch()
console.log(posts);如果要选择所有属性,则不需要使用 select ()希望它将对您有所帮助。
https://stackoverflow.com/questions/44185803
复制相似问题