简体   繁体   English

雄辩 - 多个WHERE LIKE条件

[英]Eloquent - multiple WHERE LIKE conditions

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? 如何使用Laravels Eloquent做到这一点?

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? orWhere做了什么? Does it connect the query parts with a AND? 它是否用AND连接查询部分?

No, with OR . 不,用OR As the inverse(?) the where itself does AND by default. 由于逆(?)的where本身并没有AND默认。


References 参考

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);

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

laravel 5.6 laravel 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM