[英]Filter query not working through Laravel query builder
I have a filter query where it checks for three parameters id , keywords and status .我有一个过滤器查询,它检查三个参数id 、 keywords和status 。 It's working totally fine when I run this query in MySQL but when I try to run it through Laravel's query builder it's fetching all the results of that id .
当我在 MySQL 中运行此查询时,它工作得很好,但是当我尝试通过 Laravel 的查询构建器运行它时,它正在获取该id的所有结果。
Here is the query which I'm running in MySQL(and it's working fine like I mentioned):这是我在 MySQL 中运行的查询(它运行良好,就像我提到的那样):
SELECT * FROM `poems`
WHERE book_id = 2
AND p_english_keywords LIKE '%freedom%'
AND p_status = 1
And here is the Laravel's query builder code:这是 Laravel 的查询构建器代码:
$poems = Poems
::where('book_id', '=', $filter, 'AND', 'p_english_keywords', 'LIKE', '%'.$query.'%', 'AND', 'p_status', '=', 1)
->orWhere('book_id', '=', $filter, 'AND', 'p_spanish_keywords', 'LIKE', '%'.$query.'%', 'AND', 'p_status', '=', 1)
->orWhere('book_id', '=', $filter, 'AND', 'p_german_keywords', 'LIKE', '%'.$query.'%', 'AND', 'p_status', '=', 1)
->orWhere('book_id', '=', $filter, 'AND', 'p_urdu_keywords', 'LIKE', '%'.$query.'%', 'AND', 'p_status', '=', 1)
->paginate(10);
This query looks totally fine and it's also working fine if a user removes the filter.此查询看起来完全正常,如果用户删除过滤器,它也可以正常工作。 It's searching for the keywords in user's language.
它正在搜索用户语言中的关键字。 But whenever a user applies a filter, it shows all the results from the filtered book instead of specific keywords in that book.
但是,每当用户应用过滤器时,它都会显示过滤后图书的所有结果,而不是该图书中的特定关键字。 Please Help!
请帮忙!
I'm not sure why Poems::where('book_id', '=', $filter, 'AND', 'p_english_keywords', 'LIKE', '%'.$query.'%', 'AND', 'p_status', '=', 1)
ever worked for you since that syntax is not in the documentation anywhere so I'm gonna rewrite your query.我不知道为什么
Poems::where('book_id', '=', $filter, 'AND', 'p_english_keywords', 'LIKE', '%'.$query.'%', 'AND', 'p_status', '=', 1)
曾经为您工作过,因为该语法不在任何地方的文档中,所以我将重写您的查询。
$poems = Poems::where(function ($q) use ($filter, $query) {
$q->where([
['book_id', '=', $filter],
['p_english_keywords', 'like', '%'.$query.'%'],
['p_status', '=', 1],
]);
})
->orWhere(function ($q) use ($filter, $query) {
$q->where([
['book_id', '=', $filter],
['p_spanish_keywords', 'like', '%'.$query.'%'],
['p_status', '=', 1],
]);
})
->orWhere(function ($q) use ($filter, $query) {
$q->where([
['book_id', '=', $filter],
['p_german_keywords', 'like', '%'.$query.'%'],
['p_status', '=', 1],
]);
})
->orWhere(function ($q) use ($filter, $query) {
$q->where([
['book_id', '=', $filter],
['p_urdu_keywords', 'like', '%'.$query.'%'],
['p_status', '=', 1],
]);
})
->paginate(10);
However, since you always make the the same filters ( book_id = $filter
and p_status = 1
), I think this can be reduced even further:但是,由于您总是使用相同的过滤器(
book_id = $filter
和p_status = 1
),我认为这可以进一步减少:
$poems = Poems::where([
['book_id', '=', $filter],
['p_status', '=', 1],
])
->where(function ($q) use ($query) {
$q->where('p_english_keywords', 'like', '%'.$query.'%')
->orWhere('p_spanish_keywords', 'like', '%'.$query.'%')
->orWhere('p_german_keywords', 'like', '%'.$query.'%')
->orWhere('p_urdu_keywords', 'like', '%'.$query.'%');
})
->paginate(10);
Using toSql()
, I get the following query, which I think you're looking for:使用
toSql()
,我得到以下查询,我认为您正在寻找它:
select * from `poems`
where (
`book_id` = ? and `p_status` = ?
)
and (
`p_english_keywords` like ?
or `p_spanish_keywords` like ?
or `p_german_keywords` like ?
or `p_urdu_keywords` like ?
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.