Php 包含在常量中的Larvel查询模型

Php 包含在常量中的Larvel查询模型,php,laravel,search,eloquent,Php,Laravel,Search,Eloquent,基本上,我需要做的是在数据库中进行反向搜索 我有一个模型,我们称之为Search,它包含了用户在我的网站上进行的所有搜索。给定一个特定的文本,我需要知道这些搜索中有多少与文本匹配 示例给定搜索iphone、apple、samsung和文本apple iphonex 16gb,结果将是2,因为iphone和apple匹配,但samsung不匹配 我尝试的是: $searches = Search::select('id') ->where(DB::raw("'". $

基本上,我需要做的是在数据库中进行反向搜索

我有一个模型,我们称之为
Search
,它包含了用户在我的网站上进行的所有搜索。给定一个特定的文本,我需要知道这些搜索中有多少与文本匹配

示例给定搜索
iphone
apple
samsung
和文本
apple iphonex 16gb
,结果将是2,因为
iphone
apple
匹配,但
samsung
不匹配

我尝试的是:

$searches = Search::select('id')
            ->where(DB::raw("'". $text ."' LIKE CONCAT('%', searched_text, '%')"))
            ->count();

但无论如何,它总是返回0。有一种简单的方法可以实现这一点?

因此,你有
iphone
apple
samsung
,以DB为单位,现在你要用
apple iphone X 16gb
字符串进行计数

如果要使用精确匹配,请使用
where()

如果您想像一样使用
,例如,如果您还想计算
iphone
,而不仅仅是
iphone

$words = explode(' ', $search);
$query = Search::query();
foreach ($words as $word) {
    $query = $query->orWhere('searched_text', 'like', '%' . $word . '$');
}
$counted = $query->count();

在这种情况下,它可以工作,但我需要它也为部分文本工作,而不是确切的单词,即“手机”应该与“苹果iphone X 16gb”匹配well@gbalduzzi如果使用第二个代码示例,它将匹配。
$words = explode(' ', $search);
$query = Search::query();
foreach ($words as $word) {
    $query = $query->orWhere('searched_text', 'like', '%' . $word . '$');
}
$counted = $query->count();