使用AJAX在laravel中实时搜索

时间:2017-06-12 11:21:44

标签: jquery ajax laravel laravel-5 laravel-5.4

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

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

<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:

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
    });
}

控制器:

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::post('/searching', 'SearchController@search');

Results.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>

这是我到目前为止所得到的错误:

  

未定义的变量:结果
  (查看: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)

有人可以帮助一个可怜的灵魂吗?

3 个答案:

答案 0 :(得分:1)

您在结果视图中包含结果视图,其中结果视图中的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>

修改

您正在使用路由类型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
    });
}

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

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);
}

答案 1 :(得分:0)

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

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

或者

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

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

答案 2 :(得分:0)

<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::get('/searching', 'SearchController@search');

控制器

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

<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