Php 搜索函数Laravel 5.8不适用于复数词

Php 搜索函数Laravel 5.8不适用于复数词,php,laravel,eloquent,Php,Laravel,Eloquent,我目前在我的应用程序中有一个搜索功能,它可以正常工作,但我希望它能够返回更广泛的搜索。让我解释一下 我有一个配方应用程序,每个用户都可以上传一个配方。在搜索栏中,您可以搜索配方,并根据数据库中的内容返回结果。我的问题是,如果你用复数搜索某个东西,或者拼错一点,就没有输出。示例:当食谱标题为smoothie(单数)时,搜索Smoothis(复数)。但是,如果您搜索一个菜谱,并且单词不完整(smooth、smo、sm),那么它将返回包含已完成单词的所有结果。所以我想要同样的功能,但对于字母太多或是复

我目前在我的应用程序中有一个搜索功能,它可以正常工作,但我希望它能够返回更广泛的搜索。让我解释一下

我有一个配方应用程序,每个用户都可以上传一个配方。在搜索栏中,您可以搜索配方,并根据数据库中的内容返回结果。我的问题是,如果你用复数搜索某个东西,或者拼错一点,就没有输出。示例:当食谱标题为smoothie(单数)时,搜索Smoothis(复数)。但是,如果您搜索一个菜谱,并且单词不完整(smooth、smo、sm),那么它将返回包含已完成单词的所有结果。所以我想要同样的功能,但对于字母太多或是复数的单词

以下是我目前的逻辑:


public function search(Request $request){
        $q = $request->input('search');
        $recipes = Recipe::where('title', 'LIKE', '%'.$q.'%')->orWhere('description', 'LIKE', '%'.$q.'%')->get();
        return view('recipes.results')->with('recipes', $recipes)->with('query', $q);


该函数搜索食谱的标题和说明


我很感激能得到的任何帮助

让我重新表述一下您的代码。试着读一下这本拉威尔的帮助书


让我重新解释一下你的代码。试着读一下这本拉威尔的帮助书


这会奏效,但我认为这不是正确的方法

public function search(Request $request){
    $q = $request->input('search');
    $recipes = Recipe::where(function($query) use($q){
        for($x = 3; $x < strlen($q); $x++) {
            if($x==3) {
                $query->where('title', 'LIKE', '%'.substr($q, 0, $x).'%');
            }
            else {
                $query->orWhere('title', 'LIKE', '%'.substr($q, 0, $x).'%');
            }
        }
    })->orWhere('description', 'LIKE', '%'.$q.'%')->get();
    return view('recipes.results')->with('recipes', $recipes)->with('query', $q);
公共功能搜索(请求$Request){
$q=$request->input('search');
$recipes=Recipe::where(函数($query)use($q){
对于($x=3;$xwhere('title','LIKE','%'。substr($q,0,$x)。'%');
}
否则{
$query->orWhere('title'、'LIKE'、'%'。substr($q,0,$x)。'%');
}
}
})->orWhere('description'、'LIKE'、'%.$q.'%')->get();
返回视图('recipes.results')->with('recipes',$recipes)->with('query',$q);
当用户搜索“冰沙”时,它将使用


smo,smoo,smoot,smooth,smoothi,smoothie,Smoothis这会有用的,但我认为这不是正确的方法

public function search(Request $request){
    $q = $request->input('search');
    $recipes = Recipe::where(function($query) use($q){
        for($x = 3; $x < strlen($q); $x++) {
            if($x==3) {
                $query->where('title', 'LIKE', '%'.substr($q, 0, $x).'%');
            }
            else {
                $query->orWhere('title', 'LIKE', '%'.substr($q, 0, $x).'%');
            }
        }
    })->orWhere('description', 'LIKE', '%'.$q.'%')->get();
    return view('recipes.results')->with('recipes', $recipes)->with('query', $q);
公共功能搜索(请求$Request){
$q=$request->input('search');
$recipes=Recipe::where(函数($query)use($q){
对于($x=3;$xwhere('title','LIKE','%'。substr($q,0,$x)。'%');
}
否则{
$query->orWhere('title'、'LIKE'、'%'。substr($q,0,$x)。'%');
}
}
})->orWhere('description'、'LIKE'、'%.$q.'%')->get();
返回视图('recipes.results')->with('recipes',$recipes)->with('query',$q);
当用户搜索“冰沙”时,它将使用


smo,smoo,smoot,smooth,smooth,smoothi,smoothie,smoothies

对于复数单词('title','LIKE',$q.%')可能有效,但对于拼写错误的搜索,我想你需要看看laravel scout和algolia/elastic search/或TNT search scout driver之类的免费驱动程序('title','LIKE',$q.%'))如果没有拼写错误的搜索,我想你需要看看laravel scout和algolia/elastic search这样的司机,或者TNT search scout这样的免费司机。关于拼写错误呢?关于拼写错误呢?