使用 AJAX 在 Laravel 中进行实时搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44498204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:07:22  来源:igfitidea点击:

Live search in laravel using AJAX

jqueryajaxlaravellaravel-5laravel-5.4

提问by Przemek

I want users to be able to search and display results on any page.

我希望用户能够在任何页面上搜索和显示结果。

Search box is in navbar like this:

搜索框在导航栏中,如下所示:

<form action="/search" method="get" autocomplete="off" class="navbar-form navbar-left">
    <div class="form-group">
        <input type="text" class="form-control" id="search_text" onkeyup="search_data(this.value, 'result'); placeholder="Search"></div>
        <div id="result">
            @include('results')
        </div>
    </div>
</form>

JQuery/AJAX:

jQuery/AJAX:

function search_data(search_value) {
    $.ajax({
        url: '/searching/' + search_value,
        method: 'GET'
    }).done(function(response){
        $('#results').html(response); // put the returning html in the 'results' div
    });
}

Controller:

控制器:

public function search() {
    $search_text = $_GET['text'];
    if ($search_text==NULL) {
        $data= Business::all();
    } else {
        $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
    }
    return view('results')->with('results',$data);
}

Route:

路线:

Route::post('/searching', 'SearchController@search');

Results.blade.php

结果.blade.php

<table style="width:100%">
    <tr>
        <th>Name</th>
        <th>Logo</th>
    </tr>
    @foreach( $results as $business )
    <tr>
        <td>{{ $business->logo }}</td>
        <td>{{ $business->name}}</td>
    </tr>
    @endforeach
</table>

This is what I have got so far and I am getting this error:

这是我到目前为止所得到的,我收到了这个错误:

Undefined variable: results
(View: C:\xampp\htdocs\laravel\resources\views\results.blade.php)
(View: C:\xampp\htdocs\laravel\resources\views\results.blade.php)
(View: C:\xampp\htdocs\laravel\resources\views\results.blade.php)

未定义变量:结果
(查看:C:\xampp\htdocs\laravel\resources\views\results.blade.php)
(查看:C:\xampp\htdocs\laravel\resources\views\results.blade.php)
(查看: C:\xampp\htdocs\laravel\resources\views\results.blade.php)

Can anyone kindly please help a poor soul?

任何人都可以请帮助一个可怜的灵魂吗?

回答by linktoahref

You are including the results view in the form blade, where the resultsvariable in the results view is not initially available. You could instead do a check in the results view to make sure to loop through the collection only if the data is available

您将结果视图包含在表单刀片中,结果视图中的results变量最初不可用。您可以改为在结果视图中进行检查,以确保仅在数据可用时循环遍历集合

<table style="width:100%">
    <tr>
        <th>Name</th>
        <th>Logo</th>
    </tr>
    @if (isset($results) && count($results) > 0)
    @foreach( $results as $business )
    <tr>
        <td>{{ $business->logo }}</td>
        <td>{{ $business->name}}</td>
    </tr>
    @endforeach
    @endif
</table>

EDIT

编辑

You are using Route type postand ajax method as get. You need to change your ajax method

您正在使用 Route 类型post和 ajax 方法作为get. 你需要改变你的ajax方法

function search_data(search_value) {
    $.ajax({
        url: '/searching/' + search_value,
        method: 'POST'
    }).done(function(response){
        $('#results').html(response); // put the returning html in the 'results' div
    });
}

and access that search text in your Controller as

并在您的控制器中访问该搜索文本

public function search($search = null) {
    $search_text = $search;
    if ($search_text==NULL) {
        $data= Business::all();
    } else {
        $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
    }
    return view('results')->with('results',$data);
}

回答by Md.Shahin

<form action="/search" method="get" autocomplete="off" class="navbar-form navbar-left">
<div class="form-group">
    <input type="text" class="form-control" name="search_text" id="search_text" onkeyup="search_data(this.value, 'result'); placeholder="Search"></div>
    <div id="result">
        @include('results')
    </div>
</div>

Route

路线

 Route::get('/searching', 'SearchController@search');

controller

控制器

public function search() {
$search_text = $_GET['search_text'];
if ($search_text==NULL) {
    $data= Business::all();
} else {
    $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
}
return view('results')->with('results',$data);}

Results.blade.php

结果.blade.php

<table style="width:100%">
<tr>
    <th>Name</th>
    <th>Logo</th>
</tr>
@if (isset($results) && count($results) > 0)
@foreach( $results as $business )
<tr>
    <td>{{ $business->logo }}</td>
    <td>{{ $business->name}}</td>
</tr>
@endforeach
@endif

回答by Dennis Rasmussen

I'm not entirely sure how ->with()works, but you can add data to your view helper directly with either of the two options:

我不完全确定->with()是如何工作的,但是您可以使用以下两个选项之一直接将数据添加到您的视图助手:

return view('results', ['results' => $data]);

Or

或者

return view('results', compact('data'));

Note that the last option will create a variable $data with the value of $data in your view.

请注意,最后一个选项将在您的视图中创建一个值为 $data 的变量 $data。