我想在使用 Laravel 5.1 的 SQL 查询中使用许多搜索词。这是我的代码
foreach ($searchTerms as $term) {
$query->where(function($query) use ($query, $term){
$query->where("profiles.text", "LIKE", "%{$term}%")
->orWhere("protocols.text", "LIKE", "%{$term}%");
});
}
这是我从中得到的 SQL:
where 'profiles'.'text' LIKE ? or 'protocols'.'text' LIKE ? and
'profiles'.'text' LIKE ? or 'protocols'.'text' LIKE ?
这是我需要的 SQL:
where ('profiles'.'text' LIKE ? or 'protocols'.'text' LIKE ?) and
('profiles'.'text' LIKE ? or 'protocols'.'text' LIKE ?)
如何获取正确的查询字符串?
谢谢
Pida
最佳答案
问题是,在你的闭包中你正在“使用”$query。 $query 作为参数传递,您不应在 use() block 中再次声明它。
正确的代码是:
foreach ($searchTerms as $term) {
$query->where(function($query) use ($term){
$query->where("profiles.text", "LIKE", "%{$term}%")
->orWhere("protocols.text", "LIKE", "%{$term}%");
});
}
关于php - Laravel 查询生成器 : Multiple where/orWhere and precendence of and/or,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34878251/