laravel搜索多个关键字

时间:2017-01-04 00:33:30

标签: laravel

我的网站上的搜索表单有问题。基本上,当用户将多个关键字键入搜索表单(例如名片)时,搜索结果将显示来自关键字1(例如商业)的所有图像和来自关键字2的所有图像(例如卡)。这很糟糕,搜索表单根本不相关。你可以请看下面的代码,并帮我修复它,这样搜索表单只显示两个关键字的图像(keyword1 + AND + keyword2)。

public function search($search, $category = null, $timeframe = null)
    {
        $extends = explode(' ', $search);
        if ($category) {
            $categoryId = $this->category->whereSlug($category)->first();
        }
        $images = $this->posts($category, $timeframe)->where('title', 'LIKE', '%' . $search . '%')
        ->orWhere('tags', 'LIKE', '%' . $search . '%')
        ->whereNull('deleted_at')->whereNotNull('approved_at')->orderBy('approved_at', 'desc');

        foreach ($extends as $extend) {
            if (isset($categoryId)) {
                $images->whereCategoryId($categoryId)->Where('tags', 'LIKE', '%' . $extend . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
                ->whereCategoryId($categoryId)->orWhere('title', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
                ->whereCategoryId($categoryId)->orWhere('image_description', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at');
            } else {
                $images->orWhere('tags', 'LIKE', '%' . $extend . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
                ->orWhere('title', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at')
                ->orWhere('image_description', 'LIKE', '%' . $search . '%')->whereNotNull('approved_at')->whereNull('deleted_at');
            }
        }

        return $images = $images->with('user', 'comments', 'favorites')->whereNotNull('approved_at')->whereNull('deleted_at')->paginate(perPage());
    }

1 个答案:

答案 0 :(得分:0)

当您在搜索查询之前和之后使用%符号时,您会说"如果您希望系统根据具体情况搜索并提供结果,那么请为我提供任何内容"用户已输入,您应该只使用关键短语。试试下面的代码。

     public function search($search, $category = null, $timeframe = null)
    {
        $extends = explode(' ', $search);
        if ($category) {
            $categoryId = $this->category->whereSlug($category)->first();
        }
        $images = $this->posts($category, $timeframe)
        ->where('title', 'LIKE',  "$search")
        ->orWhere('tags', 'LIKE', "$search")
        ->whereNull('deleted_at')
        ->whereNotNull('approved_at')
        ->orderBy('approved_at', 'desc');

        foreach ($extends as $extend) {
            if (isset($categoryId)) {
                $images->whereCategoryId($categoryId)->Where('tags', 'LIKE', "$search")
                ->whereNotNull('approved_at')->whereNull('deleted_at')
                ->whereCategoryId($categoryId)->orWhere('title', 'LIKE', "$search")
                ->whereNotNull('approved_at')->whereNull('deleted_at')
                ->whereCategoryId($categoryId)
->orWhere('image_description', 'LIKE', "$search")
                ->whereNotNull('approved_at')->whereNull('deleted_at');
            } 
            else {
                $images->orWhere('tags', 'LIKE', "$extend")
                ->whereNotNull('approved_at')->whereNull('deleted_at')
                ->orWhere('title', 'LIKE', "$search")
                ->whereNotNull('approved_at')->whereNull('deleted_at')
                ->orWhere('image_description', 'LIKE', "$search")
                ->whereNotNull('approved_at')->whereNull('deleted_at');
            }
        }
        return $images = $images->with('user', 'comments', 'favorites')
        ->whereNotNull('approved_at')
        ->whereNull('deleted_at')
        ->paginate(perPage());
    }