laravel Eloquent - 多个 WHERE LIKE 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42053525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eloquent - multiple WHERE LIKE conditions
提问by nameless
I'm currently trying to implement a search in my db for keywords. Therefore I split a string seperated by a comma, so I get an array containing a variable number of elements (one keyword).
我目前正在尝试在我的数据库中搜索关键字。因此,我拆分了一个由逗号分隔的字符串,因此我得到了一个包含可变数量元素(一个关键字)的数组。
I know that I can use a eloquent construct like that:
我知道我可以使用像这样的雄辩结构:
$products = Product::where([['keywords', 'LIKE', %samsung%], [keywords, 'LIKE', 's7']])->paginate(15);
to find a product with the keywords samsung,galaxy,s7
.
找到带有关键字的产品samsung,galaxy,s7
。
Now I need this thing but automatically generated for a variable number of search query parts, so for every keyword in the array I need to add one ['keywords', 'LIKE', '...']...
现在我需要这个东西,但会自动为可变数量的搜索查询部分生成,所以对于数组中的每个关键字,我需要添加一个 ['keywords', 'LIKE', '...'] ...
How can I do this with Laravels Eloquent?
我怎样才能用 Laravel Eloquent 做到这一点?
回答by Chay22
Use closure. First, make sure you stored list of keywords into array or the like. Then ...
使用闭包。首先,确保您将关键字列表存储到数组等中。然后 ...
$keywords = ['samsung', 's7', 'what else'];
$products = Product::where(function ($query) use ($keywords) {
foreach ($keywords as $keyword) {
$query->orWhere('keyword', 'like', $keyword);
}
})->paginate(15);
other example
另一个例子
$keywords = [
['name', 'LIKE', $searchQuery],
['category_id', '=', $selectedSubcategory],
];
$products = Product::where(function ($query) use ($keywords) {
foreach ($keywords as $keyword) {
$query->where($keyword);
}
})->paginate(15);
other than other
除了其他
$keywords = [
['name', 'LIKE', $searchQuery],
['category_id', '=', $selectedSubcategory],
['please_id', '=', $learnPhpArray],
];
$products = Product::query();
foreach ($keywords as $keyword) {
$products = $products->where($keyword);
}
return $products->paginate(15);
What does the orWhere do? Does it connect the query parts with a AND?
orWhere 有什么作用?它是否使用 AND 连接查询部分?
No, with OR
. As the inverse(?) the where
itself does AND
by default.
不,与OR
。作为逆(?)它where
本身AND
默认情况下。
References
参考
回答by Ulrik McArdle
Isn't it just to generate the array? Or am I misunderstanding the question?
不就是生成数组吗?还是我误解了这个问题?
<?php
$keys = ['samsung', 'lg', 'sony', 'nokia', 'apple'];
$keywords = [];
foreach($keys as $key){
$keywords[] = ['keywords', 'LIKE', '%'.$key.'%'];
}
$products = Product::where($keywords)->paginate(15);
回答by apokryfos
You can do this:
你可以这样做:
$query = "Samsung galaxy S7"; // Or whatever the query is
$words = preg_split("/[ ,.]/",$query); //Split on space comma or dot. Maybe need more?
$queries = array_map(function ($word) {
return [ "keywords", "LIKE", "%$word%" ];
}, $words); //Transform single words to array of queries
$products = Product::where($queries)->paginate(15);
Check how the first part of the code works at: https://eval.in/730772
检查代码的第一部分如何工作:https: //eval.in/730772
回答by V? Ng?c Nh?t
laravel 5.6
拉拉维尔 5.6
$keyWords = KeyWordModel::all();
$keyWordQuerys = [];
foreach ($keyWords as $key => $item) {
$keyWordQuerys[$key] = ['title', 'like', '%'.$item->name.'%'];
}
$contents = ContentModel::query();
foreach ($keyWordQuerys as $key => $item) {
$contents = $contents->orwhere([$item]);
}
$contents = $contents->orderBy('pubDate', 'DESC')->get();