laravel搜索用空格分隔的多个单词 [英] laravel search multiple words separated by space
问题描述
我是新的laravel查询构建器,我想搜索输入字段中输入的多个单词,例如,如果我输入jhon doe,我想得到任何包含jhon或doe的列
我已经看过/尝试过使用php MySQL的解决方案,但无法适应查询构建器
// 1。爆炸关键词之间的空间
// 2。使用foreach将查询一起发送
$ query =select * from users where;
$ keywordRaw =jhon doe;
$ keywords = explode('',$ keywordRaw);
foreach($ keywords as $ keyword){
$ query。=first_name LIKE'%+ $ keyword +%'OR;
}
如何使用查询构建器执行此操作
这是我到目前为止,这样做的正确方法是什么?
$ keywordRaw =jhon doe;
//如何爆炸这些话,并附加他们的相应查询
$ users = User :: select('users。*')
- > where('first_name' ,'LIKE','%'。$ keywordRaw。'%')
请帮助,谢谢提前
这是你如何使用 Query\Builder
,但首先需要附加一些注释:
//用户可以意外提供双重空间,或者有目的地提供:
$ string ='john doe';
//所以爆炸你得到这个:
explode('',$ string);
数组(
0 =>'john',
1 =>'',
2 =>'doe'
)
//现在,如果你使用LIKE'%''value''%',你会得到这样的:
从表中选择*,其名称如'%john%'或名称像'%%'或。 ..
说,你显然不能依靠 explode
因为在上述情况下你会得到所有的行。
所以,这是你应该做的:
$ string ='john doe';
// split on 1+ whitespace&忽略空(例如,尾随空格)
$ 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();
中有
的关闭,因为它是一个很好的做法,将你的或
子句包含在括号中。例如,如果您的用户
模型使用 SoftDeletingScope
,则不会执行我的建议,您的整个查询将被弄乱。
I am new to laravel query builder, I want to search multiple words entered in an input field for example if I type "jhon doe" I want to get any column that contains jhon or doe
I have seen/tried solutions using php MySQL but can't able to adapt to query builder
//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 ";
}
how do I do this using query builder
this is what i have so far, what is the proper way of doing this,
$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.'%')
please help, thanks in advance
This is how you do it with Query\Builder
, but first some additional notes:
// 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 ...
That said, you obviously can't rely on explode
because in the above case you would get all the rows.
So, this is what you should do:
$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();
There is closure in the where
because it is a good practice to wrap your or where
clauses in parentheses. For example if your User
model used SoftDeletingScope
and you would not do what I suggested, your whole query would be messed up.
这篇关于laravel搜索用空格分隔的多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!