【问题标题】:Search functionality only uses one parameter搜索功能仅使用一个参数
【发布时间】:2026-01-22 21:00:01
【问题描述】:

:)

我已经为想要浏览招聘信息的用户实现了一个搜索功能,它看起来像这样:

https://i.stack.imgur.com/wGcW6.png

它的代码如下所示:

public function index(Request $request)
    {
if ($word = $request->get('s')) {

                $postsQuery = $postsQuery
                    ->where('titel', 'like', "%{$word}%")
                    ->where('isActive', 1)
                    ->where('is_Canceled', 0)
                    ->orWhere('id', '=', $word);

            }

            if ($location = $request->get('standort') and $request->get('standort') != 'alle') {
                $postsQuery = $postsQuery->where('Standort', $location);
            }

            if ($dep = $request->get('abteilung') and $request->get('abteilung') != 'alle') {
                $postsQuery = $postsQuery->where('abteilung_name', $dep);
            }

            $posts = $postsQuery->orderBy('titel')->get();

            if ($posts->count() == 0) {

                return view('posts.overview', ['posts' => $posts, 'standorts' => $standorts, 'abteilungs' => $abteilungs])
                    ->withErrors(['error' => 'Es wurde kein Treffer gefunden, versuchen Sie es mit anderen Suchparametern']);
            }

            return view('posts.overview', ['posts' => $posts, 'standorts' => $standorts, 'abteilungs' => $abteilungs]);
        }

我的问题是只搜索一个参数。因此,当用户输入搜索词“IT”+ 位置 (Standort) 或部门 (Abteilung) 时,只会搜索搜索词。

我在 2 小时内尝试了无数不同的东西,但我就是无法让它发挥作用。如果您有任何提示,请告诉我!

编辑:当我忽略 orWhere('id', '=', $word) 时,它可以工作,但我必须将它留在里面。但我无法将其更改为正常的 where(),因为它不会返回任何匹配项。

【问题讨论】:

    标签: php laravel search eloquent


    【解决方案1】:

    你做到了:

    where a and b and c or d

    但你想要:

    where (a and b and c) or d

    https://laravel.com/docs/8.x/queries#logical-grouping

    粗略地说,你应该努力构建这样的东西:

    $posts = Post::query()
        ->where(function (Builder $query) {
            $query->where('clauseA', '=', 1)
                ->where('clauseB', '=', 2)
                ->where('clauseC', '=', 3);
        })
        ->orWhere('id', '=', $word)
        ->get();
    

    【讨论】:

    • 是的!我不知道我有多么妄想,或者我是否正在失明。我现在以另一种方式修复了它,我应该发布答案吗?几周前有人告诉我另一个问题太具体了,所以我不应该发布答案。这也太具体了吧?
    • 分享吧,何乐而不为:)
    • 是的,你是对的。上次我刚投了反对票。
    • 你还在吗?我会对您的解决方案有疑问! :)
    【解决方案2】:
    public function index(Request $request)
        {
    if ($word = $request->get('s')) {
    
                    if (is_numeric($word)) {
                        $postsQuery = $postsQuery
                            ->where('id', '=', $word)
                            ->where('isActive', 1)
                            ->where('is_Canceled', 0);
    
                    }  else {
                        $postsQuery = $postsQuery
                            ->where('titel', 'like', "%{$word}%")
                            ->where('isActive', 1)
                            ->where('is_Canceled', 0);
                    }
    
                }
    
                if ($location = $request->get('standort') and $request->get('standort') != 'alle') {
                    $postsQuery = $postsQuery->where('Standort', $location);
                }
    
                if ($dep = $request->get('abteilung') and $request->get('abteilung') != 'alle') {
                    $postsQuery = $postsQuery->where('abteilung_name', $dep);
                }
    
                $posts = $postsQuery->orderBy('titel')->get();
    
                if ($posts->count() == 0) {
    
                    return view('posts.overview', ['posts' => $posts, 'standorts' => $standorts, 'abteilungs' => $abteilungs])
                        ->withErrors(['error' => 'Es wurde kein Treffer gefunden, versuchen Sie es mit anderen Suchparametern']);
                }
    
                return view('posts.overview', ['posts' => $posts, 'standorts' => $standorts, 'abteilungs' => $abteilungs]);
            }
    

    这不是一个优雅的解决方案,但我只是检查搜索词是数字还是单词,然后继续进行相应的条件和搜索功能

    【讨论】:

    • 是的,这几乎是一个不同的问题,但我明白你的想法。我可能会做几个函数:searchById()searchByKeyword(),这两个函数都会重用提取到第三个函数的公共逻辑buildSearchQuery()
    • 或者,也许这也有帮助:laravel.com/docs/8.x/queries#conditional-clauses
    • @AndriusRimkus 是的,它是完整的意大利面条代码哈哈 :D 但我只需要它工作到明天,然后我还有 4 周的时间来重构和优化。所以我会一直这样做到明天,然后根据您的意见进行重构,非常感谢您花时间回答!