Laravel查询模型包含在常量中

问题描述 投票:2回答:1

我需要做的是,基本上,与DB中的搜索相反。

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

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

我尝试的是:

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

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

php laravel search eloquent
1个回答
3
投票

所以,你在DB中有qazxsw poi,qazxsw poi和qazxsw poi,现在你想用iphone字符串计算。

如果要使用完全匹配,请使用apple

samsung

如果你想使用apple iphone X 16gb,例如,如果你想计算whereIn()而不仅仅是Search::whereIn('searched_text', explode(' ', $search))->count();

like
© www.soinside.com 2019 - 2024. All rights reserved.