Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel搜索多个由空格分隔的单词_Php_Mysql_Laravel_Eloquent_Query Builder - Fatal编程技术网

Php laravel搜索多个由空格分隔的单词

Php laravel搜索多个由空格分隔的单词,php,mysql,laravel,eloquent,query-builder,Php,Mysql,Laravel,Eloquent,Query Builder,我不熟悉laravel查询生成器,我想搜索输入字段中输入的多个单词,例如,如果我键入“jhon doe”,我想得到任何包含jhon或doe的列 我见过/尝试过使用php MySQL的解决方案,但无法适应查询生成器 //1. exploding the space between the keywords //2. using foreach apend the query together $query = "select * from users where"; $keywordRaw

我不熟悉laravel查询生成器,我想搜索输入字段中输入的多个单词,例如,如果我键入“jhon doe”,我想得到任何包含jhon或doe的列

我见过/尝试过使用php MySQL的解决方案,但无法适应查询生成器

//1. exploding the space between the keywords 

//2. using foreach apend the query together

$query = "select * from users where";

$keywordRaw = "jhon doe";
$keywords = explode(' ', $keywordRaw );
foreach ($keywords as $keyword){
$query.= " first_name LIKE '%" + $keyword +"%' OR ";
}
如何使用查询生成器执行此操作

//1. exploding the space between the keywords 

//2. using foreach apend the query together

$query = "select * from users where";

$keywordRaw = "jhon doe";
$keywords = explode(' ', $keywordRaw );
foreach ($keywords as $keyword){
$query.= " first_name LIKE '%" + $keyword +"%' OR ";
}
这就是我目前所拥有的,正确的做法是什么

$keywordRaw = "jhon doe";
//how do I explode this words and append them along with their appropriate query
$users = User::select('users.*')
->where('first_name', 'LIKE', '%'.$keywordRaw.'%')

请帮忙,提前谢谢你可以试试下面的方法

$keywordRaw = "jhon doe";
$bindArr = explode(" ", $keywordRaw);

$query = "select * from users where";    
foreach ($i = 0; $i < count($bindArr); $i++) {
    if ($i == 0) {
        $query.= ' first_name LIKE "%?%"';
    } else {        
        $query.= ' or first_name LIKE "%?%"';
    }
}

$sth = $dbh->prepare($query);
$sth->execute($bindArr);
$keywordRaw=“jhon doe”;
$bindArr=explode(“,$keywordRaw”);
$query=“从用户中选择*”;
foreach($i=0;$iprepare($query);
$sth->execute($bindArr);

试试这个..

   $searchQuery = "jhon doe"; 
$searchTerms = explode(" ", $searchQuery); // Split the words
$users = User::whereIn('FirstName', $searchTerms)->get();
print_r($users);

这会起作用。Where将从您输入的关键字中搜索名字。

这是使用
Query\Builder
执行此操作的方式,但首先需要注意一些其他注意事项:

// user can provide double space by accident, or on purpose:
$string = 'john  doe';

// so with explode you get this:
explode(' ', $string);
array(
  0 => 'john',
  1 => '',
  2 => 'doe'
)

// Now if you go with LIKE '%'.value.'%', you get this:
select * from table where name like '%john%' or name like '%%' or ...
也就是说,您显然不能依赖于
explode
,因为在上述情况下,您将获得所有行

所以,这就是你应该做的:

$string = 'john  doe';

// split on 1+ whitespace & ignore empty (eg. trailing space)
$searchValues = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY); 

$users = User::where(function ($q) use ($searchValues) {
  foreach ($searchValues as $value) {
    $q->orWhere('name', 'like', "%{$value}%");
  }
})->get();

where
中有闭包,因为将
或where
子句用括号括起来是一种很好的做法。例如,如果您的
用户
型号使用了
SoftDeletingScope
,而您不按照我的建议去做,那么您的整个查询就会一团糟。

您是否考虑过在您的名字列上使用全文索引

您可以使用Laravel迁移创建此索引,但需要使用SQL语句:

DB::statement('ALTER TABLE users ADD FULLTEXT(first_name);');
然后,您可以对该字段运行非常高级的搜索,如下所示:

$keywordRaw = "john doe";
$keywords   = explode(' ', $keywordRaw);
$users   = User::select("*")
              ->whereRaw("MATCH (first_name)
                          against (? in boolean mode)",[$keywords])
              ->get();
$keywords   = '+'.explode(' +', $keywordRaw);
$users = User::select("*")
               ->selectRaw("MATCH (first_name)
                            against (? in boolean mode)
                            AS relevance",[$keywords])
               ->whereRaw("MATCH (first_name)
                           against (? in boolean mode)",[$keywords])
               ->orderBy('relevance','DESC')
               ->get();
将匹配包含“john”或“doe”字样的记录;请注意,这种方法将匹配整个单词,而不是子字符串(如果使用LIKE,则可能是这种情况)

如果要查找包含所有单词的记录,应在每个关键字前面加一个“+”,如下所示:

$keywordRaw = "john doe";
$keywords   = explode(' ', $keywordRaw);
$users   = User::select("*")
              ->whereRaw("MATCH (first_name)
                          against (? in boolean mode)",[$keywords])
              ->get();
$keywords   = '+'.explode(' +', $keywordRaw);
$users = User::select("*")
               ->selectRaw("MATCH (first_name)
                            against (? in boolean mode)
                            AS relevance",[$keywords])
               ->whereRaw("MATCH (first_name)
                           against (? in boolean mode)",[$keywords])
               ->orderBy('relevance','DESC')
               ->get();
您甚至可以根据相关性进行排序,尽管这对于您的需求来说可能有些过头了(与“所有”搜索无关)。大概是这样的:

$keywordRaw = "john doe";
$keywords   = explode(' ', $keywordRaw);
$users   = User::select("*")
              ->whereRaw("MATCH (first_name)
                          against (? in boolean mode)",[$keywords])
              ->get();
$keywords   = '+'.explode(' +', $keywordRaw);
$users = User::select("*")
               ->selectRaw("MATCH (first_name)
                            against (? in boolean mode)
                            AS relevance",[$keywords])
               ->whereRaw("MATCH (first_name)
                           against (? in boolean mode)",[$keywords])
               ->orderBy('relevance','DESC')
               ->get();
这里有一篇很好的文章介绍了这种通用方法:

您可以使用此软件包

如果你不想使用这个软件包,也可以这样做

$keywordRaw = "jhon doe";
$users = User::where('first_name', 'LIKE', $keywordRaw.'%')
->orWhere('first_name', 'LIKE', '% '.$keywordRaw.'%')
->get();

使用“orWhere”将获得每个关键字的结果,而不是关键字1+关键字2的结果……因此,这一切都取决于您要查找的内容,其中是明确的,即
=
,而OP想要
如%value%
,因此它不是唯一的。字符串中的双空格或其他空格也会使其无效。谢谢@Jarek,我刚刚用我的其他联接表实现了它,对其进行分页,效果非常好,再次感谢