Laravel 喜欢 Eloquent 的数组值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27696212/
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
Laravel Like Eloquent with array value?
提问by user1469734
How can I do a Like-query, with multiple values to search for?
我怎样才能做一个 Like 查询,要搜索多个值?
$searchWords = explode(' ', Input::get('search'));
Then I get an array of words that were given for the search.
然后我得到一组用于搜索的单词。
How can I pass it in this:
我怎样才能通过它:
$pages = Page::where('content', 'LIKE', '%'.$singleWord.'%')->distinct()->get();
A loop can't work, then it overwrites the $pages always; then it keeps always the latest search:
循环不起作用,然后它总是覆盖 $pages;然后它总是保持最新的搜索:
foreach($searchWords as $word){
$pages = Page::where('content', 'LIKE', '%'.$word.'%')->distinct()->get();
}
回答by lukasgeiter
A loop is the solution but you only need to add the where condition without executing the query
循环是解决方案,但您只需要添加 where 条件而不执行查询
$pages = Page::query();
foreach($searchWords as $word){
$pages->orWhere('content', 'LIKE', '%'.$word.'%');
}
$pages = $pages->distinct()->get();
回答by Amine_Dev
i will code it for you :
我会为你编码:
$pages = Page->where(function($query) use($word){
foreach($searchWords as $word){
$query->orWhere('content', 'LIKE', '%'.$word.'%');
}
})->get();
you can try that , i hope that will help :)
你可以试试,我希望会有所帮助:)