简体   繁体   English

搜索查询在Laravel中不起作用

[英]search query not working in laravel

I need to return the results of what the user write in the search bar, but when i press search to test it nothing happens. 我需要返回用户在搜索栏中写的结果,但是当我按search进行测试时,什么也没有发生。

this is how i created the search bar 这就是我创建搜索栏的方式

<form class="form-inline" role="search" action="{{ route('search-source') }}" method="POST">
    {{ csrf_field() }}
    <div class="form-group">
        <input type="text" class="form-control" name="searchbar" placeholder="Find a source!">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

in my controller i have the following code to find what the user has entered in the search bar. 在我的控制器中,我有以下代码来查找用户在搜索栏中输入的内容。 It searches the datebase for a title thats like whats written in the search bar. 它在日期数据库中搜索类似于搜索栏中写的标题的标题。 If nothing is found it return's an error (this is working). 如果未找到任何内容,则返回错误(正在运行)。 if a row higher than 0 return it need to bring that result back to the page 如果行高于0,则需要将结果返回到页面

public function searchSource(Request $request){
    $this->validate($request,[
        'searchbar' => 'required',
    ]);

    $find = $request['searchbar'];

    $query = Source::where('title','LIKE', '%'.$find.'%')->where('user_id', Auth::user()->id)->get();

    if(count($query) > 0) {
        return redirect()->route('sources-index')->with(['query' => $query]);
    }else{
        return redirect()->route('sources-index')->with('customerror', 'nothing found, try a different searchterm.');
    }
}

back on the page it need to get the result if there is asked for a searchterm, else it need to give everything of the database 返回页面,如果需要搜索条件,则需要获取结果,否则需要提供数据库的所有内容

@if(!empty($query))
    @foreach($query as $querys)
        <tr>
            <td class="source-cell">
                {{$querys->title}}
            </td>

            <td>
                {{$querys->summary}}
            </td>

            <td>
                {{$querys->course->name}}
            </td>
        </tr>
    @endforeach
@else
    @foreach ($sources as $source)
        <tr>
            <td class="source-cell">
                {{$source->title}}
            </td>

            <td>
                {{$source->summary}}
            </td>

            <td>
                {{$source->course->name}}
            </td>
        </tr>
    @endforeach
@endif

I tried dd() on a couple of places and everything worked fine, except on the page, i tried dd($query) after the if statement nothing happend. 我在几个地方都尝试了dd() ,并且一切正常,除了在页面上,在if语句什么都没发生之后,我尝试了dd($query)

I tried it after the foreach nothing happend. 在foreach没有任何反应之后,我尝试了一下。

But when i tried it before the if statement i got an error 但是当我在if语句之前尝试它时,出现错误

Undefined variable: query (View: ...\\resources\\views\\sources\\index.blade.php) 未定义变量:查询(视图:... \\ resources \\ views \\ sources \\ index.blade.php)

What am i doing wrong. 我究竟做错了什么。

In your above code $query is misspelled 在上面的代码中, $query拼写错误

Change 更改

@if(!empty($uery))

to

@if(!empty($query))

Modify your controller like below : 如下修改控制器:

public function searchSource(Request $request){
    $this->validate($request,[
        'searchbar' => 'required',
    ]);
    $find = $request['searchbar'];
    $query = Source::where('title','LIKE', '%'.$find.'%')->where('user_id', Auth::user()->id)->get();
    return redirect()->route('sources-index')->with(['query' => $query]);

}

Modify your view like below : 修改您的视图,如下所示:

@if(!empty($uery))
    @foreach($query as $querys)
        <tr>
            <td class="source-cell">
                {{$querys->title}}
            </td>

            <td>
                {{$querys->summary}}
            </td>

            <td>
                {{$querys->course->name}}
            </td>
        </tr>
    @endforeach
@else
nothing found, try a different searchterm.
@endif
@if(!empty($sources))
    @foreach ($sources as $source)
        <tr>
            <td class="source-cell">
                {{$source->title}}
            </td>

            <td>
                {{$source->summary}}
            </td>

            <td>
                {{$source->course->name}}
            </td>
        </tr>
    @endforeach
@else
nothing found, try a different searchterm.
@endif

Because in your else part redirection does not have $query variable. 因为在您的else部分中,重定向没有$ query变量。

@ Peter Arents能否请您尝试以下操作

$query = Source::where('title', 'LIKE', "%$find%")->where('user_id', '=', Auth::user()->id);

redirect()->with() uses session to store the data, so you'll need to change the code to: redirect()->with()使用会话存储数据,因此您需要将代码更改为:

@if (session()->has('query'))
    @foreach(session('query') as $querys)

Or, you need to change redirect() part to this: 或者,您需要将redirect()部分更改为此:

return view('query.index', compact('query'));

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

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