Laravel喜欢雄辩的数组值吗? [英] Laravel Like Eloquent with array value?

查看:39
本文介绍了Laravel喜欢雄辩的数组值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用多个要搜索的值进行喜欢"查询?

How can I do a Like-query, with multiple values to search for?

$searchWords = explode(' ', Input::get('search'));

然后,我得到了一系列用于搜索的单词.

Then I get an array of words that were given for the search.

我该如何通过它:

$pages = Page::where('content', 'LIKE', '%'.$singleWord.'%')->distinct()->get();

循环无法正常工作,然后总是覆盖$ pages;那么它将始终保持最新搜索:

A loop can't work, then it overwrites the $pages always; then it keeps always the latest search:

foreach($searchWords as $word){
    $pages = Page::where('content', 'LIKE', '%'.$word.'%')->distinct()->get();
}

推荐答案

解决方案是循环,但您只需要添加where条件而不执行查询

A loop is the solution but you only need to add the where condition without executing the query

$pages = Page::query();
foreach($searchWords as $word){
    $pages->orWhere('content', 'LIKE', '%'.$word.'%');
}
$pages = $pages->distinct()->get();

这篇关于Laravel喜欢雄辩的数组值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆