简体   繁体   English

删除搜索词后显示所有内容

[英]Displaying everything after search term is deleted

I have a site that displays every post that is stored in a database with a search bar at the top.我有一个网站,显示存储在数据库中的每篇文章,顶部有一个搜索栏。 Obviously when the user first visits the page no search term is applied and all posts are visible.显然,当用户第一次访问该页面时,没有应用搜索词并且所有帖子都是可见的。

When the user then searches for a word and presses the search button everything works fine.当用户然后搜索一个单词并按下搜索按钮时,一切正常。 However, when you delete the string from the search bar and press search again all posts are gone, of course going back with mouse buttons works and reloading aswell.但是,当您从搜索栏中删除字符串并再次按搜索时,所有帖子都消失了,当然,使用鼠标按钮返回并重新加载也可以。 But I would like to achieve that when the empty search bar is submitted all posts appear.但我想实现,当提交空搜索栏时,所有帖子都会出现。

This is the form:这是表格:

<form action=" {{ route('overview') }}" method="get">
    <div>
        <input placeholder="Schlagwort" type="text" id="s" name="s" value="{{ request()->get('s') }}">
    </div>
    <button type="submit">Suchen</button>
</form>

This is the controller with the search function:这是 controller 搜索 function:


public function index(Request $request) {

    $posts = Post::get();

    if($request->has('s')) {
        $query = strtolower($request->get('s'));
        $posts = $posts->filter(function ($post) use ($query) {
            if (Str::contains(strtolower($post->Titel), $query)) {
                return true;
            }
            return false;
        });

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

I tried this when trying to achieve the 'empty search bar display everything "feature" '我在尝试实现'空搜索栏显示所有“功能”'时尝试了这个

public function index(Request $request) {
    $posts = Post::get();

    if($request->has('s')) {
        $query = strtolower($request->get('s'));
        $posts = $posts->filter(function ($post) use ($query) {
            if (Str::contains(strtolower($post->Titel), $query)) {
                return true;
            }
            return false;
        });

    } else if ($request == ' ') {
        return view('posts.overview', ['posts' => $posts]);
    }
    return view('posts.overview', ['posts' => $posts]);
}

As well swapped out with else if ($request == null)...以及与else if ($request == null)...

You are checking if the s input is present, when instead you want to check if it has a value.您正在检查s输入是否存在,而您想检查它是否有值。 Use the filled() method for that.为此使用filled()方法

In addition, you are needlessly fetching everything from the database and then filtering in PHP.此外,您不必要地从数据库中获取所有内容,然后在 PHP 中进行过滤。 Instead, build a query that checks for the input value using the when() method .相反,构建一个使用when()方法检查输入值的查询。

public function index(Request $request)
{    
    $posts = Post::query()
        ->when(
            $request->filled('s'),
            fn ($q) => $q->where('title', 'like', $request->s)
        )
        ->get();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM